package com.cm.central.control.service.customer.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.plugin.pojo.dtos.dataarea.DataAreaDTO;
import com.cm.common.plugin.service.dataarea.IDataAreaService;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.central.control.dao.customer.ICustomerDao;
import com.cm.central.control.pojo.dtos.customer.CustomerDTO;
import com.cm.central.control.pojo.vos.customer.CustomerVO;
import com.cm.central.control.service.customer.ICustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: CustomerServiceImpl
 * @Description: 客户管理
 * @Author: WenG
 * @Date: 2020-07-30 19:00
 * @Version: 1.0
 **/
@Service
public class CustomerServiceImpl extends AbstractService implements ICustomerService {

    @Autowired
    private IDataAreaService dataAreaService;
    @Autowired
    private ICustomerDao customerDao;

    @Override
    public SuccessResult saveCustomer(CustomerVO customerVO) throws Exception {
        saveCustomerInfo(null, customerVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveCustomerByToken(String token, CustomerVO customerVO) throws Exception {
        saveCustomerInfo(token, customerVO);
        return new SuccessResult();
    }

    @Override
    public String saveCustomerReturnId(CustomerVO customerVO) throws Exception {
        return saveCustomerInfoReturnId(null, customerVO);
    }

    @Override
    public String saveCustomerByTokenReturnId(String token, CustomerVO customerVO) throws Exception {
        return saveCustomerInfoReturnId(token, customerVO);
    }

    /**
     * 新增客户管理
     *
     * @param token
     * @param customerVO
     * @throws Exception
     */
    private void saveCustomerInfo(String token, CustomerVO customerVO) throws Exception {
        saveCustomerInfoReturnId(token, customerVO);
    }

    /**
     * 新增客户管理
     *
     * @param token
     * @param customerVO
     * @return customerId
     * @throws Exception
     */
    private String saveCustomerInfoReturnId(String token, CustomerVO customerVO) throws Exception {
        String customerId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(customerVO);
        setArea(customerVO, params);
        params.put("customerId", customerId);
        if (token != null) {
            setSaveInfo(token, params);
        } else {
            setSaveInfo(params);
        }
        customerDao.saveCustomer(params);
        return customerId;
    }

    @Override
    public SuccessResult removeCustomer(String ids) throws RemoveException {
        removeCustomerInfo(null, ids);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeCustomerByToken(String token, String ids) throws RemoveException {
        removeCustomerInfo(token, ids);
        return new SuccessResult();
    }

    /**
     * 删除客户管理
     *
     * @param token
     * @param ids
     */
    private void removeCustomerInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("customerIds", Arrays.asList(ids.split("_")));
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        customerDao.removeCustomer(params);
    }

    @Override
    public void deleteCustomer(String ids) throws RemoveException {
        Map<String, Object> params = getHashMap(3);
        params.put("customerIds", Arrays.asList(ids.split("_")));
        customerDao.deleteCustomer(params);
    }

    @Override
    public SuccessResult updateCustomer(String customerId, CustomerVO customerVO) throws Exception {
        updateCustomerInfo(null, customerId, customerVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateCustomerByToken(String token, String customerId, CustomerVO customerVO) throws Exception {
        updateCustomerInfo(token, customerId, customerVO);
        return new SuccessResult();
    }

    /**
     * 修改客户管理
     *
     * @param token
     * @param customerId
     * @param customerVO
     */
    private void updateCustomerInfo(String token, String customerId, CustomerVO customerVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(customerVO);
        setArea(customerVO, params);
        params.put("customerId", customerId);
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        customerDao.updateCustomer(params);
    }

    /**
     * 设置地区
     *
     * @param customerVO
     * @param params
     */
    private void setArea(CustomerVO customerVO, Map<String, Object> params) {
        if (!StringUtils.isBlank(customerVO.getArea1Id())) {
            DataAreaDTO dataAreaDTO = dataAreaService.getAreaById(customerVO.getArea1Id());
            params.put("area1Code", dataAreaDTO.getAreaCode());
            params.put("area1Name", dataAreaDTO.getAreaName());
        } else {
            params.put("area1Code", "");
            params.put("area1Name", "");
        }
        if (!StringUtils.isBlank(customerVO.getArea2Id())) {
            DataAreaDTO dataAreaDTO = dataAreaService.getAreaById(customerVO.getArea2Id());
            params.put("area2Code", dataAreaDTO.getAreaCode());
            params.put("area2Name", dataAreaDTO.getAreaName());
        } else {
            params.put("area2Code", "");
            params.put("area2Name", "");
        }
        if (!StringUtils.isBlank(customerVO.getArea3Id())) {
            DataAreaDTO dataAreaDTO = dataAreaService.getAreaById(customerVO.getArea3Id());
            params.put("area3Code", dataAreaDTO.getAreaCode());
            params.put("area3Name", dataAreaDTO.getAreaName());
        } else {
            params.put("area3Code", "");
            params.put("area3Name", "");
        }
        if (!StringUtils.isBlank(customerVO.getArea4Id())) {
            DataAreaDTO dataAreaDTO = dataAreaService.getAreaById(customerVO.getArea4Id());
            params.put("area4Code", dataAreaDTO.getAreaCode());
            params.put("area4Name", dataAreaDTO.getAreaName());
        } else {
            params.put("area4Code", "");
            params.put("area4Name", "");
        }
        if (!StringUtils.isBlank(customerVO.getArea5Id())) {
            DataAreaDTO dataAreaDTO = dataAreaService.getAreaById(customerVO.getArea5Id());
            params.put("area5Code", dataAreaDTO.getAreaCode());
            params.put("area5Name", dataAreaDTO.getAreaName());
        } else {
            params.put("area5Code", "");
            params.put("area5Name", "");
        }
    }

    @Override
    public CustomerDTO getCustomerById(String customerId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("customerId", customerId);
        return customerDao.getCustomer(params);
    }

    @Override
    public List<CustomerDTO> listCustomer(Map<String, Object> params) throws SearchException {
        return customerDao.listCustomer(params);
    }

    @Override
    public SuccessResultList<List<CustomerDTO>> listPageCustomer(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<CustomerDTO> customerDTOs = customerDao.listCustomer(page.getParams());
        PageInfo<CustomerDTO> pageInfo = new PageInfo<>(customerDTOs);
        return new SuccessResultList<>(customerDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer countNumberCustomer(Map<String, Object> params) throws SearchException {
        Integer count = customerDao.countCustomer(params);
        return count == null ? 0 : count;
    }

    @Override
    public SuccessResultData<Integer> countCustomer(Map<String, Object> params) throws SearchException {
        return new SuccessResultData<>(countNumberCustomer(params));
    }

}