package com.dk.mdm.service.mst;

import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.infrastructure.enums.ErrorCodeEnum;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseCodeEnum;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.mst.CustomerConvert;
import com.dk.mdm.infrastructure.util.AuthUtils;
import com.dk.mdm.model.pojo.mst.Customer;
import com.dk.mdm.mapper.mst.CustomerMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.query.mst.CustomerQuery;
import com.dk.mdm.model.response.mst.CustomerResponse;
import com.dk.mdm.model.vo.mst.CustomerVO;
import com.dk.mdm.service.common.CommonService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CustomerService extends BaseService<Customer> {

    @Override
    public BaseMapper<Customer> getRepository() {
        return customerMapper;
    }

    @Autowired
    private CustomerMapper customerMapper;


    @Autowired
    private CustomerConvert customerConvert;

    @Autowired
    private CommonService commonService;

    @Autowired
    private OrgService orgService;


    @Autowired
    private AuthUtils authUtils;

    /**
     * @desc : 重写主键
     * @author : 于继渤
     * @date : 2024/2/29 20:29
     */
    @Override
    public String getPrimaryKey() {
        return "cus_id";
    }


    /**
     * @desc : 查询
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Pagination
    public ResponseResultVO<PageList<CustomerResponse>> selectByCond(CustomerQuery customerQuery) {
        return super.mergeListWithCount(customerQuery, customerMapper.selectByCond(customerQuery),
                customerMapper.countByCond(customerQuery));
    }

    /**
     * @desc : 新建
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> insert(CustomerVO customerVO) {
        Customer customer = customerConvert.convertToPo(customerVO);
        //设置编码
        Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMER.getName(), true);
        customer.setCusId(uniqueNoteCode.get("outId").toString());
        customer.setCusCode(uniqueNoteCode.get("outNote").toString());

        // 同商户 电话不同
        Long count = customerMapper.countByCond(
                new CustomerQuery().setCpId(customer.getCpId()).setCusPhone(customer.getCusPhone()).setFlgValid(true)
        );
        if (count > 0) {
            return ResponseResultUtil.error(ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getCode(),
                    ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getMessage());
        }
        List<String> followStaff = new ArrayList<>();
        String staffId = authUtils.getStaff().getStaffId();
        followStaff.add(staffId);
        customer.setFollowStaffs(followStaff);
        if (customer.getSaleStatus() == null) {
            //新建默认就是潜客
            customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
        }

        //报备人
        customer.setReportStaff(staffId);
        //报备日期
        customer.setReportTime(LocalDateTime.now());
        ResponseResultVO<?> insert = super.insert(customer);
        if (insert.getCode() == ResponseCodeEnum.SUCCESS.getCode()) {
            return ResponseResultUtil.success(customer);
        } else {
            return insert;
        }

    }

    /**
     * @desc : 编辑
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> update(CustomerVO customerVO) {
        Customer customer = customerConvert.convertToPo(customerVO);
        super.updateByUuid(customer);
        return ResponseResultUtil.success();
    }

    /**
     * @desc : 跟据id查询
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    public ResponseResultVO<CustomerResponse> selectByIdRespone( String id) {
        CustomerResponse customerResponse = customerMapper.selectByIdRespone(id);
        if (customerResponse == null) {
            return ResponseResultUtil.error(ResponseCodeEnum.SELECT_NULL);
        }
        return ResponseResultUtil.success(customerResponse);
    }
    /**
     * @desc : 不分页查询
     * @author : 于继渤
     * @date : 2023/1/5 9:39
     */
    public ResponseResultVO<List<CustomerResponse>> selectByCondNoPage(CustomerQuery customerQuery) {
        List<CustomerResponse> customerResponses = customerMapper.selectByCond(customerQuery);
        return ResponseResultUtil.success(customerResponses);
    }
}
