package com.ag.dynamicmodule.basecode.controller;

import com.ag.dynamicmodule.basecode.dao.CClientDao;
import com.ag.dynamicmodule.basecode.dao.CClientInvoiceDao;
import com.ag.dynamicmodule.basecode.dao.CClientLinkmanDao;
import com.ag.dynamicmodule.basecode.dao.CClientTypeRelaDataDao;
import com.ag.dynamicmodule.basecode.pojo.CClient;
import com.ag.dynamicmodule.basecode.pojo.CClientInvoice;
import com.ag.dynamicmodule.basecode.pojo.CClientLinkman;
import com.ag.dynamicmodule.basecode.pojo.CClientTypeRelaData;
import com.ag.util.CommonUtil;
import com.ag.dynamicmodule.service.IBaseServiceImpl;
import com.ag.dynamicmodule.util.PinyinUtils;
import com.ag.log.entity.LogParam;
import com.ag.log.OperLogServcie;
import com.alibaba.fastjson.JSON;
import com.wb.util.SysUtil;
import com.wb.util.WebUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/30.
 */
@Component
public class CClientController {
    @Autowired
    CClientDao cClientDao;
    @Autowired
    CClientInvoiceDao cClientInvoiceDao;
    @Autowired
    CClientLinkmanDao cClientLinkmanDao;
    @Autowired
    CClientTypeRelaDataDao cClientTypeRelaDataDao;
    @Autowired
    private IBaseServiceImpl baseService;
    @Autowired
    private OperLogServcie logServcie;


    /**
     * 保存数据
     * @param object
     * @param response
     * @param request
     */
    @Transactional(rollbackFor = Exception.class)
    public  void  saveMethod(JSONObject object, HttpServletResponse response, HttpServletRequest request) throws IOException{
        //客户基本信息
        CClient cClient = JSON.parseObject(object.getString("cClientData"),CClient.class);
        //发票信息
        CClientInvoice cClientInvoice = JSON.parseObject(object.getString("cClientInvoiceData"),CClientInvoice.class);

        //汉子提示设置成客户名称的拼音首字母简写
        String pinyin = "";
        if(!StringUtils.isEmpty(cClient.getClientName())){
            pinyin = PinyinUtils.getAlpha(cClient.getClientName());
        }
        cClient.setInitials(pinyin);
        if(!StringUtils.isEmpty(cClient.getId())){//修改
            baseService.setParameterValue(cClient,request);
            String id = cClient.getId();//客户ID
            //统计客户代码为空时，设置成客户代码的值
            if(StringUtils.isEmpty(cClient.getCountClientCode())){
                cClient.setCountClientCode(cClient.getClientCode());
            }
            cClientDao.update(cClient);
            if(!StringUtils.isEmpty(cClientInvoice.getId())){//发票修改
                baseService.setParameterValue(cClientInvoice,request);
                cClientInvoice.setClientCode(cClient.getClientCode());
                cClientInvoiceDao.update(cClientInvoice);
            }else{//发票新增
                baseService.setParameterValue(cClientInvoice,request);
                cClientInvoice.setId(SysUtil.getId());
                cClientInvoice.setClientCode(cClient.getClientCode());
                cClientInvoiceDao.insert(cClientInvoice);
            }
            //删除客户类型，待重新添加
            cClientTypeRelaDataDao.deleteByClientCode(cClient.getClientCode());
            LogParam logParam = new LogParam(object, cClient.getClientName(), "client", "U", "cClientData");
            logServcie.saveLogPanel(logParam, response);
         }else{//新增
            baseService.setParameterValue(cClient,request);
            String newId = SysUtil.getId();
            cClient.setId(newId);
            cClient.setClientCode(newId);
            //统计客户代码为空时，设置成客户代码的值
            if(StringUtils.isEmpty(cClient.getCountClientCode())){
                cClient.setCountClientCode(cClient.getClientCode());
            }
            cClientDao.insert(cClient);
            baseService.setParameterValue(cClientInvoice,request);
            cClientInvoice.setId(SysUtil.getId());
            cClientInvoice.setClientCode(newId);
            cClientInvoiceDao.insert(cClientInvoice);
            LogParam logParam = new LogParam(object, cClient.getClientName(), "client", "C", "cClientData");
            logServcie.saveLogPanel(logParam, response);
        }
        List<CClientTypeRelaData> typeRelas = proTypeCode(request,cClient.getClientCode());
        if(!(typeRelas==null||typeRelas.size()==0)){
            //循环添加客户的类型
            for(int i = 0;i < typeRelas.size();i++){
                cClientTypeRelaDataDao.insert(typeRelas.get(i));
            }
        }
        saveLinkManMethod(object,response,request,cClient.getClientCode());
        try {
            WebUtil.send(response, cClient.getClientCode());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Transactional(rollbackFor = Exception.class)
    public void saveLinkManMethod(JSONObject object, HttpServletResponse response, HttpServletRequest request,String id) {
        List<CClientLinkman> createList = CommonUtil.getBeanlist(object, "cClientLinkmanData", "create", CClientLinkman.class);
        List<CClientLinkman> updateList = CommonUtil.getBeanlist(object, "cClientLinkmanData", "update", CClientLinkman.class);
        List<CClientLinkman> destroyList = CommonUtil.getBeanlist(object, "cClientLinkmanData", "destroy", CClientLinkman.class);
        for (CClientLinkman clientLinkman:createList){
            clientLinkman.setClientCode(id);
            cClientLinkmanDao.insert(clientLinkman);
        }
        for (CClientLinkman clientLinkman:updateList){
            clientLinkman.setClientCode(id);
            cClientLinkmanDao.update(clientLinkman);
        }
        for (CClientLinkman clientLinkman:destroyList) {
            String linkId = clientLinkman.getId();
            cClientLinkmanDao.delete(linkId);
        }
    }
    /**
     * 删除记录
     * @param object
     * @param response
     * @param request
     */
    @Transactional(rollbackFor = Exception.class)
    public void delCClient(JSONObject object, HttpServletResponse response, HttpServletRequest request) {
        String ids = request.getParameter("ids");
        String clientCode = request.getParameter("clientCode");
        cClientDao.delete(ids);
        cClientInvoiceDao.delete(clientCode);
        cClientTypeRelaDataDao.deleteByClientCode(clientCode);
        cClientLinkmanDao.deleteByClientCode(clientCode);

    }
    /**
     * Description: 客户类型数据拼接
     * @param request
     * @return
     * Note: Nothing much
     */
    public List<CClientTypeRelaData> proTypeCode(HttpServletRequest request,String id){
        List<CClientTypeRelaData> list = new ArrayList<>();
        String typeCodes = request.getParameter("typeCodes");
        if(!StringUtils.isEmpty(typeCodes)){
            String[] typeCodeArray= typeCodes.split(",");
            for (int i=0;i<typeCodeArray.length;i++) {
                CClientTypeRelaData cClientTypeRelaData = new CClientTypeRelaData();
                cClientTypeRelaData.setId(SysUtil.getId());
                cClientTypeRelaData.setClientCode(id);
                cClientTypeRelaData.setClientTypeCode(typeCodeArray[i]);
                list.add(cClientTypeRelaData);
            }
        }
        return list;
    }
}
