package cn.superdesk.uniorder.service.unified.impl;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.marketing.department.UnifiedAreaCityVirt;
import cn.superdesk.uniorder.dao.entity.organisation.SysOrganization;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedCustomer;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedCustomerContactsKey;
import cn.superdesk.uniorder.dao.entity.unified.UnifiedProject;
import cn.superdesk.uniorder.dao.mapper.marketing.department.UnifiedAreaCityVirtMapper;
import cn.superdesk.uniorder.dao.mapper.marketing.department.UnifiedAreaCityVirtMapperEx;
import cn.superdesk.uniorder.dao.mapper.organisation.SysOrganizationMapperEx;
import cn.superdesk.uniorder.dao.mapper.unified.UnifiedCustomerContactsMapper;
import cn.superdesk.uniorder.dao.mapper.unified.UnifiedCustomerMapper;
import cn.superdesk.uniorder.dao.mapper.unified.UnifiedProjectMapper;
import cn.superdesk.uniorder.dto.vo.unified.UnifiedCustomerVo;
import cn.superdesk.uniorder.service.unified.UnifiedCustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

@Service
public class UnifiedCustomerServiceImpl implements UnifiedCustomerService {

    @Autowired
    private UnifiedCustomerMapper         unifiedCustomerMapper;
    @Autowired
    private UnifiedProjectMapper          unifiedProjectMapper;
    @Autowired
    private SysOrganizationMapperEx       sysOrganizationMapperEx;
    @Autowired
    private UnifiedCustomerContactsMapper unifiedCustomerContactsMapper;

    @Autowired
    private UnifiedAreaCityVirtMapper   unifiedAreaCityVirtMapper;
    @Autowired
    private UnifiedAreaCityVirtMapperEx unifiedAreaCityVirtMapperEx;


    @Override
    public int insertSelective(UnifiedCustomer t) {

        return unifiedCustomerMapper.insert(t);
    }

    @Override
    public int updateByPrimaryKeySelective(UnifiedCustomer t) {

        return unifiedCustomerMapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public void deleteObj(Long id) {

        unifiedCustomerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public UnifiedCustomer findById(Long id) {

        return unifiedCustomerMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<UnifiedCustomer> findAll(Map<String, Object> queryMap) {

        return null;
    }

    @Override
    public PageInfo<UnifiedCustomer> findByPaging(Map<String, Object> params, Integer pageNo, Integer pageSize) {

        return null;
    }

    /***
     * 废弃
     */
    @Transactional
    @Override
    public UnifiedCustomer addObjTran(UnifiedCustomer customer, List<UnifiedCustomerContactsKey> contactskeys) {

        UnifiedProject unifiedProject = unifiedProjectMapper.selectByPrimaryKey(customer.getProjectId());
        if (unifiedProject == null) {
            List<SysOrganization> orgs = sysOrganizationMapperEx.findByOutOrgId(customer.getProjectId());
            SysOrganization       org  = orgs.get(0);
            unifiedProject = new UnifiedProject();
            unifiedProject.setCreateTime(DateUtil.date());
            unifiedProject.setCreateUserId(customer.getCreateUserId());
            unifiedProject.setProjectId(customer.getProjectId());
            unifiedProject.setProjectName(org.getOrganizationName());
            unifiedProject.setProjectNo(org.getOrganizationNo());
            unifiedProject.setUpdateTime(customer.getUpdateTime());
            unifiedProjectMapper.insert(unifiedProject);
        }
        unifiedCustomerMapper.insert(customer);
        for (UnifiedCustomerContactsKey unifiedCustomerContactsKey : contactskeys) {
            unifiedCustomerContactsMapper.insert(unifiedCustomerContactsKey);
        }
        return customer;
    }

    /***
     * 废弃
     */
    @Override
    public UnifiedCustomer updateObjTran(UnifiedCustomer customer, List<UnifiedCustomerContactsKey> contactskeys) {

        unifiedCustomerMapper.updateByPrimaryKeySelective(customer);
        if (contactskeys.size() > 0) {
            unifiedCustomerContactsMapper.deleteByVirtId(customer.getVirtId());
            for (UnifiedCustomerContactsKey unifiedCustomerContactsKey : contactskeys) {
                unifiedCustomerContactsMapper.insert(unifiedCustomerContactsKey);
            }
        }

        return customer;
    }

    @Override
    public R<List<UnifiedCustomerVo>> getDataByPage(Map<String, Object> params, int pageNo,
                                                    int pageSize) {

        PageHelper.startPage(pageNo, pageSize);

        List<UnifiedCustomerVo> ls = unifiedCustomerMapper.getData(params);

        PageInfo<UnifiedCustomerVo> page = new PageInfo<UnifiedCustomerVo>(ls);
        PageHelper.clearPage();
        return new R<List<UnifiedCustomerVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), ls);

    }

    @Transactional
    @Override
    public UnifiedCustomer updateObjTranNew(UnifiedCustomer customer, UnifiedAreaCityVirt av) {

        unifiedCustomerMapper.updateByPrimaryKeySelective(customer);
        if (av.getAreaCityId() == null) {
            unifiedAreaCityVirtMapper.deleteByPrimaryKey(customer.getVirtId());
        } else {
            UnifiedAreaCityVirt sv = unifiedAreaCityVirtMapper.selectByPrimaryKey(av.getVirtId());
            if (sv != null) {
                unifiedAreaCityVirtMapper.updateByPrimaryKey(av);
            } else {
                unifiedAreaCityVirtMapper.insert(av);
            }
        }

        return customer;
    }

    @Transactional
    @Override
    public UnifiedCustomer addObjTranNew(UnifiedCustomer customer, UnifiedAreaCityVirt av) {

        UnifiedProject unifiedProject = unifiedProjectMapper.selectByPrimaryKey(customer.getProjectId());

        if (unifiedProject == null) {

            List<SysOrganization> orgs = sysOrganizationMapperEx.findByOutOrgId(customer.getProjectId());
            SysOrganization       org  = orgs.get(0);

            unifiedProject = new UnifiedProject();

            unifiedProject.setCreateTime(DateUtil.date());
            unifiedProject.setCreateUserId(customer.getCreateUserId());
            unifiedProject.setProjectId(customer.getProjectId());
            unifiedProject.setProjectName(org.getOrganizationName());
            unifiedProject.setProjectNo(org.getOrganizationNo());
            unifiedProject.setUpdateTime(customer.getUpdateTime());

            unifiedProjectMapper.insert(unifiedProject);
        }

        unifiedCustomerMapper.insert(customer);

        UnifiedAreaCityVirt sv = unifiedAreaCityVirtMapper.selectByPrimaryKey(av.getVirtId());

        if (sv != null) {
            unifiedAreaCityVirtMapper.updateByPrimaryKey(av);
        } else {
            unifiedAreaCityVirtMapper.insert(av);
        }

        return customer;
    }

}
