package com.slodon.b2b2c.model.member.advich;

import com.slodon.b2b2c.core.constant.EnquiryConst;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.dao.read.member.MemberEnquiryReadMapper;
import com.slodon.b2b2c.dao.write.member.MemberEnquiryAssignWriteMapper;
import com.slodon.b2b2c.dao.write.system.EnquiryWriteMapper;
import com.slodon.b2b2c.member.example.MemberEnquiryAssignExample;
import com.slodon.b2b2c.member.pojo.MemberEnquiryAssign;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.system.example.EnquiryExample;
import com.slodon.b2b2c.system.pojo.Enquiry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author cyan
 */
@Component
@Slf4j
public class MemberEnquiryAssignModel {

    @Resource
    private MemberEnquiryAssignWriteMapper memberEnquiryAssignWriteMapper;

    @Resource
    private MemberEnquiryReadMapper memberEnquiryReadMapper;

    @Value("${geoip.static.city.mmdb}")
    private String geoipCityMmdb;

    @Resource
    private EnquiryWriteMapper enquiryWriteMapper;

    /**
     * 询盘分配
     *
     * @param enquiryAssign
     * @return
     */
    public Integer saveEnquiryAssign(MemberEnquiryAssign enquiryAssign) {
        MemberEnquiryAssignExample enquiryAssignExample = new MemberEnquiryAssignExample();
        enquiryAssignExample.setEnquiryId(enquiryAssign.getEnquiryId());
        memberEnquiryAssignWriteMapper.deleteByExample(enquiryAssignExample);

        enquiryAssign.setCreateTime(new Date());
        int count = memberEnquiryAssignWriteMapper.insert(enquiryAssign);
        AssertUtil.notNullOrZero(count, "询盘分配失败，请重试");

        //更新询盘信息
        updateEnquiryRecord(enquiryAssign.getEnquiryId(), enquiryAssign.getOperateUid(), enquiryAssign.getOperateName());
        return count;
    }

    /**
     * 更新询盘信息
     *
     * @param enquiryId
     * @param uid
     * @param username
     * @return
     */
    public void updateEnquiryRecord(Long enquiryId, Long uid, String username) {
        EnquiryExample enquiryExample = new EnquiryExample();
        EnquiryExample.Criteria criteria = enquiryExample.createCriteria();
        criteria.andIdEqualTo(enquiryId);
        Enquiry enquiry = new Enquiry();
        enquiry.setStatus(EnquiryConst.ENQUIRY_ASSIGN);
        enquiry.setLastUid(uid);
        enquiry.setLastUsername(username);
        enquiry.setLastTime(new Date());
        enquiryWriteMapper.updateByExampleSelective(enquiry, enquiryExample);
    }

}
