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

import com.slodon.b2b2c.core.constant.EnquiryConst;
import com.slodon.b2b2c.core.constant.NumConstant;
import com.slodon.b2b2c.core.response.PagerInfo;
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.read.member.MemberEnquiryTrackReadMapper;
import com.slodon.b2b2c.dao.write.member.MemberEnquiryAssignWriteMapper;
import com.slodon.b2b2c.dao.write.member.MemberEnquiryTrackWriteMapper;
import com.slodon.b2b2c.dao.write.system.EnquiryWriteMapper;
import com.slodon.b2b2c.member.example.MemberEnquiryAssignExample;
import com.slodon.b2b2c.member.example.MemberEnquiryTrackExample;
import com.slodon.b2b2c.member.pojo.MemberEnquiryAssign;
import com.slodon.b2b2c.member.pojo.MemberEnquiryTrack;
import com.slodon.b2b2c.seller.example.VendorExample;
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;
import java.util.List;

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

    @Resource
    private MemberEnquiryTrackWriteMapper memberEnquiryTrackWriteMapper;

    @Resource
    private EnquiryWriteMapper enquiryWriteMapper;

    @Resource
    private MemberEnquiryTrackReadMapper memberEnquiryTrackReadMapper;

    /**
     * 询盘跟踪
     *
     * @param enquiryTrack
     * @return
     */
    public Integer saveEnquiryTrack(MemberEnquiryTrack enquiryTrack) {
        enquiryTrack.setCreateTime(new Date());
        int count = memberEnquiryTrackWriteMapper.insert(enquiryTrack);
        AssertUtil.notNullOrZero(count, "询盘跟踪失败，请重试");

        //更新询盘记录
        updateEnquiryRecord(enquiryTrack.getEnquiryId(), enquiryTrack.getVendorId(), enquiryTrack.getVendorName());
        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.setLastUid(uid);
        enquiry.setLastUsername(username);
        enquiry.setLastTime(new Date());
        enquiryWriteMapper.updateByExampleSelective(enquiry, enquiryExample);
    }

    /**
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<MemberEnquiryTrack> getTrackList(MemberEnquiryTrackExample example, PagerInfo pager) {
        List<MemberEnquiryTrack> trackList;
        if (pager != null) {
            pager.setRowsCount(memberEnquiryTrackReadMapper.countByExample(example));
            trackList = memberEnquiryTrackReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            trackList = memberEnquiryTrackReadMapper.listByExample(example);
        }
        return trackList;
    }

    /**
     * 标记为垃圾询盘
     *
     * @param enquiryId
     * @return
     */
    public Integer makeEnquiryGarbage(Long enquiryId, Long uid, String username) {
        EnquiryExample enquiryExample = new EnquiryExample();
        EnquiryExample.Criteria criteria = enquiryExample.createCriteria();
        criteria.andIdEqualTo(enquiryId);
        Enquiry enquiry = new Enquiry();
        enquiry.setValidStatus(EnquiryConst.ENQUIRY_INVALID);
        enquiry.setLastUid(uid);
        enquiry.setLastUsername(username);
        enquiry.setLastTime(new Date());
        return enquiryWriteMapper.updateByExampleSelective(enquiry, enquiryExample);
    }

    /**
     * 标记为垃圾询盘
     *
     * @param enquiryId
     * @return
     */
    public Integer cancleMakeEnquiryGarbage(Long enquiryId, Long uid, String username) {
        EnquiryExample enquiryExample = new EnquiryExample();
        EnquiryExample.Criteria criteria = enquiryExample.createCriteria();
        criteria.andIdEqualTo(enquiryId);
        Enquiry enquiry = new Enquiry();
        enquiry.setValidStatus(EnquiryConst.ENQUIRY_VALID);
        enquiry.setLastUid(uid);
        enquiry.setLastUsername(username);
        enquiry.setLastTime(new Date());
        return enquiryWriteMapper.updateByExampleSelective(enquiry, enquiryExample);
    }
}
