package org.jeecg.modules.basedata.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.basedata.entity.FdCustomer;
import org.jeecg.modules.basedata.entity.FdCustomerContacts;
import org.jeecg.modules.basedata.mapper.FdCustomerMapper;
import org.jeecg.modules.basedata.service.IFdCustomerContactsService;
import org.jeecg.modules.basedata.service.IFdCustomerService;
import org.jeecg.modules.system.service.ISysSerialPatternService;
import org.jeecg.modules.system.vo.CallResult;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 客户档案
 * @author： jeecg-boot
 * @date：   2019-05-05
 * @version： V1.0
 */
@Service
@Transactional
public class FdCustomerServiceImpl extends ServiceImpl<FdCustomerMapper, FdCustomer> implements IFdCustomerService {

    @Resource
    private FdCustomerMapper fdCustomerMapper;

    @Resource
    private IFdCustomerService fdCustomerService;

    @Resource
    private IFdCustomerContactsService fdCustomerContactsService;

    @Resource
    private ISysSerialPatternService iSysSerialPatternService;

    @Override
    public IPage<FdCustomer> findByPage(IPage<FdCustomer> page, QueryWrapper<FdCustomer> queryWrapper) {
        return fdCustomerMapper.findByPage(page,queryWrapper);
    }

    @Override
    public FdCustomer CustomerOdd(String name) {
        return fdCustomerMapper.Customer(name);
    }
    
    /**
     * @Author 谭吉
     * @Description //TODO 客户表、联系人表添加
     * @Date 11:23 2019/5/15
     * @Param [customerm]
     * @return boolean
     **/
    @Override
    public void saveCustomer(FdCustomer customerm) {
        if(StringUtils.isNotBlank(customerm.getId())){
            //修改
            fdCustomerService.updateById(customerm);
            //删除旧联系人
            LambdaQueryWrapper<FdCustomerContacts> fdCustomerContactsLambdaQueryWrapper=new LambdaQueryWrapper<>();
            fdCustomerContactsLambdaQueryWrapper.eq(FdCustomerContacts::getPkCustomerId,customerm.getId());
            fdCustomerContactsLambdaQueryWrapper.eq(FdCustomerContacts::getDelFlag,"0");
            fdCustomerContactsService.remove(fdCustomerContactsLambdaQueryWrapper);
        }else{//新增
            //编码规则 {KH}+6
            CallResult<String> resultSerial = iSysSerialPatternService.getNextSerial(customerm, "fd_customer", "code", true);
            customerm.setCode(resultSerial.getContent());
            fdCustomerService.save(customerm);
        }
        // LambdaQueryWrapper<FdCustomer> query = new LambdaQueryWrapper<>();
        // query.eq(FdCustomer::getName,customerm.getName()).eq(FdCustomer::getDelFlag,"0");
        // List<FdCustomer> customerList = fdCustomerService.list(query);
        // for ( FdCustomer fdCustomer : customerList ) {
        //     customerm.setId(fdCustomer.getId());
        // }
        if( customerm.getFdCustomerContactsList() != null ){
            Integer isdufault = 0;
            for ( FdCustomerContacts fdCustomerContact : customerm.getFdCustomerContactsList() ) {
                if ( Integer.parseInt(fdCustomerContact.getIsDefault()) == 1 ){
                    isdufault = isdufault + 1;
                }
            }
            if( isdufault == 1 ){
                for ( FdCustomerContacts fdCustomerContact : customerm.getFdCustomerContactsList() ) {
                    fdCustomerContact.setId(null);
                    fdCustomerContact.setPkCustomerId(customerm.getId());
                    fdCustomerContact.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
                    fdCustomerContactsService.save(fdCustomerContact);
                }
            }else if(isdufault > 1){
                throw new RuntimeException("只能有一个默认联系人！");
            }else{
                throw new RuntimeException("必须添加一个默认联系人！");
            }
        }else {
            throw new RuntimeException("联系人不能为空！");
        }
    }
    
    /**
     * @Author 谭吉
     * @Description //TODO 客户表、联系人表修改
     * @Date 11:23 2019/5/15
     * @Param [customerm]
     * @return boolean
     **/
    @Override
    public boolean updateCustomer(FdCustomer customerm) {
        //  查询出相关联系人
        LambdaQueryWrapper<FdCustomerContacts> query = new LambdaQueryWrapper<>();
        query.eq(FdCustomerContacts::getPkCustomerId,customerm.getId());
        List<FdCustomerContacts> list = fdCustomerContactsService .list(query);
        for ( FdCustomerContacts fdCustomerContacts : list ) {
            //  修改前删除客户相关的联系人
            fdCustomerContactsService.removeById(fdCustomerContacts);
        }
        boolean customer = fdCustomerService.updateById(customerm);
        FdCustomer customerms = CustomerOdd(customerm.getName());
        boolean customerContact = false;
        for ( FdCustomerContacts fdCustomerContact : customerm.getFdCustomerContactsList() ) {
            fdCustomerContact.setDelFlag(CommonConstant.DEL_FLAG_0.toString());
            fdCustomerContact.setPkCustomerId(customerm.getId());
            fdCustomerContactsService.save(fdCustomerContact);
        }
        if( customer == true && customerContact == true){
            return true;
        }
        return false;
    }

    @Override
    public void cusAndContactsDelete(String id) {
        //删除客户联系人
        LambdaQueryWrapper<FdCustomerContacts> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(FdCustomerContacts::getPkCustomerId,id);
        fdCustomerContactsService.remove(queryWrapper);
        //删除档案
        fdCustomerService.removeById(id);
    }

}
