package com.sifang.modules.completionrectification.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.dao.CompletionDao;
import com.sifang.modules.completionrectification.dao.RectificationDao;
import com.sifang.modules.completionrectification.dao.RectificationInfoDetailDao;
import com.sifang.modules.completionrectification.entity.CrmCompletionInfo;
import com.sifang.modules.completionrectification.entity.CrmRectificationInfo;
import com.sifang.modules.completionrectification.entity.CrmRectificationInfoDetail;
import com.sifang.modules.completionrectification.vo.CrmRectificationDetailVo;
import com.sifang.modules.customer.Service.CustomerEntryMsgService;
import com.sifang.modules.customer.constant.CustomerEntryConstants;
import com.sifang.modules.partner.service.GuangfaOperatingService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.util.LayuiPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class RectificationService extends BaseService {

    @Autowired
    private RectificationDao rectificationDao;

    @Autowired
    private RectificationInfoDetailDao rectificationInfoDetailDao;

    @Autowired
    private GuangfaOperatingService guangfaOperatingService;

    @Autowired
    private CustomerEntryMsgService msgService;

    @Autowired
    private CompletionDao completionDao;

    public void getRectificationOrderList(LayuiPage<Record> page, Map<String, String> map) {
        List<Record> list = rectificationDao.getRectificationOrderList(page, map);
        page.setData(list);
        page.setCount(rectificationDao.countRectificationOrderList(map));
    }

    public CrmRectificationInfo findRectificationById(String id) {
        return rectificationDao.findRectificationById(id);
    }

    public List<Record> getRectificationOrderStationList(String inputPieceId) {
        String[] inputPieceIds = inputPieceId.split(",");
        List<Record> list = rectificationDao.getRectificationOrderStationList(inputPieceIds);
        for(Record record : list){
            List<Record> deList = rectificationInfoDetailDao.getRectificationDetailByInputPieceId(record.getStr("input_piece_id"));
            record.set("detail",deList);
        }
        return list;
    }

    public Record getRectificationOrderDetail(String id) {
        return rectificationDao.getRectificationOrderDetail(id);
    }

    @Transactional(rollbackFor=Exception.class)
    public Result saveRectificationInfo(JSONObject jsonObject){
        Result result=new Result();
        User user = UserUtils.getUser();
        Date date = new Date();
        Map map = JSON.parseObject(JSON.toJSONString(jsonObject), Map.class);
        //调用提交整改接口
        List<CrmRectificationDetailVo> list = (List<CrmRectificationDetailVo>) map.get("stationInfos");
        String state = guangfaOperatingService.submitReformedInfo(map);
        if("200".equals(state)){
            CrmRectificationInfo info = rectificationDao.findRectificationByRectificationNo(map.get("reformedNo").toString());
            info.setFianlUpdateTime(date);
            info.setCompletionTime(new Date());
            //修改整改基本信息
            rectificationDao.save(info);
            for(CrmRectificationDetailVo vo : list){
                CrmRectificationInfoDetail detail = rectificationInfoDetailDao.findRectificationById(vo.getId());
                detail.setRectificationAfterImg(vo.getReformAfterPic());
                detail.setRectificationTerm(vo.getItemDes());
                detail.setRectificationFeedback(vo.getFeedbackDes());
                //修改整改详情
                rectificationInfoDetailDao.save(detail);
            }
            CrmCompletionInfo crmCompletionInfo = completionDao.findCompletionByCompletionNo(info.getCompletionNo());
            String[] idArr = crmCompletionInfo.getEntryId().split(",");
            //过程信息
            for(String entryId : idArr) {
                msgService.saveCustomerEntryMsg(user, entryId, CustomerEntryConstants.REFORMED_INCOMING, "竣工进件提交");
            }
        }
        result.setCode("200");
        return result;
    }

}