package com.winsun.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.util.DateUtil;
import com.winsun.bean.SalesList;
import com.winsun.mapper.SalesListMapper;
import com.winsun.service.ISalesListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author chengfengluo
 * @date 2021-03-08 17:40
 */
@Service
public class SalesListServiceImpl extends ServiceImpl<SalesListMapper, SalesList> implements ISalesListService {

    @Autowired
    private SalesListMapper salesListMapper;

    /**
     * 添加销售清单
     * @return
     */
    public ResponseData<String> addSalesList(SalesList salesList) {

        // 获取4个月内记录号码数据是否重复
        Wrapper<SalesList> wrapper = new EntityWrapper<>();
        wrapper.eq("order_phone", salesList.getOrderPhone());
        wrapper.ge("create_time", this.getFourMonthYearAgoData());
        List<SalesList> salesOrders = salesListMapper.selectList(wrapper);

        // 获取重复提示
        String error = "";
        String info = "";
        String repeatSchoolName = "";
        if (salesOrders != null && salesOrders.size() != 0) {
            for(SalesList databaseSaleList : salesOrders) {
                if (salesList.getSalesSchool().equals(databaseSaleList.getSalesSchool())) {
                    error = "该放号号码已经在该销售学校登记";
                }else{
                    info = "该放号号码与其它学校有重复登记，请后期进行核对;";
                    repeatSchoolName += "号码在" + databaseSaleList.getSalesSchool() + "学校有重复记录，请后期进行核对;";
                }
            }
        }

        // 插入销售清单
        if (StringUtils.isBlank(error)) {

            Integer insert = salesListMapper.insert(salesList);

            // 成功更新重复学校
            if (StringUtils.isNotBlank(info)) {
                salesOrders.add(salesList);
                this.updateRepeatSchool(salesList.getOrderPhone(), "该号码在其他学校重复登记", repeatSchoolName);
                return ResponseData.success(info);
            }
            return ResponseData.success("登记成功");
        }else{
            return ResponseData.error(error);
        }
    }

    /**
     * 更新重复学校
     * @param orderPhone
     * @param remark  审核说明
     * @param dataErrorRemark 数据错误提示
     */
    public void updateRepeatSchool(String orderPhone, String remark, String dataErrorRemark) {

        Wrapper<SalesList> wrapper = new EntityWrapper<>();
        wrapper.eq("order_phone", orderPhone);
        SalesList updateSalesList = new SalesList();
        updateSalesList.setStatus("2");
        updateSalesList.setRemark(remark);
        updateSalesList.setDataErrorRemark(dataErrorRemark);
        salesListMapper.update(updateSalesList, wrapper);
    }

    // 获取4个月前的日期
    public String getFourMonthYearAgoData() {
        Calendar fourMonthago = Calendar.getInstance();
        fourMonthago.setTime(new Date());
        fourMonthago.add(Calendar.MONTH, -4);
        return DateUtil.format(fourMonthago.getTime(), DateUtil.YYYY_MM_DD_HH_MM_SS);
    }
}
