package com.jwipc.finance.logic.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jwipc.finance.entity.AccountStatementDetail;
import com.jwipc.finance.entity.AccountStatementOpt;
import com.jwipc.finance.logic.AccountStatementOptLogic;
import com.jwipc.finance.service.IAccountStatementOptService;
import com.jwipc.security.dto.CurrentUserDTO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author shenyang.ou
 * @date 2024/3/4 16:17
 * @desc
 */
@Service
@RequiredArgsConstructor
public class AccountStatementOptLogicImpl implements AccountStatementOptLogic {
    private final IAccountStatementOptService accountStatementOptService;

    /**
     * 添加无操作内容的记录
     *
     * @param optType 操作类型 1:新增 2:编辑 3:删除
     * @param asId 对账单ID
     * @param asNo 对账单号
     * @param asdIdList 对账单明细ID集合
     */
    @Override
    public void addNotContentRecord(Integer optType, Long asId, String asNo, List<Long> asdIdList, CurrentUserDTO currentUser) {
            if(CollectionUtils.isNotEmpty(asdIdList)){
                List<AccountStatementOpt> optList = Lists.newArrayList();
                for(Long asdId: asdIdList){
                    AccountStatementOpt opt = new AccountStatementOpt();
                    opt.setAsId(asId);
                    opt.setAsNo(asNo);
                    opt.setAsdId(asdId);
                    opt.setOptType(optType);
                    opt.setCreateUserId(currentUser.getUserId());
                    opt.setCreateUserCode(currentUser.getLoginName());
                    opt.setCreateUserName(currentUser.getName());
                    opt.setCreateTime(LocalDateTime.now());
                    optList.add(opt);
                }
                accountStatementOptService.saveBatch(optList);
            }else {
                AccountStatementOpt opt = new AccountStatementOpt();
                opt.setAsId(asId);
                opt.setAsNo(asNo);
                opt.setAsdId(null);
                opt.setOptType(optType);
                opt.setCreateUserId(currentUser.getUserId());
                opt.setCreateUserCode(currentUser.getLoginName());
                opt.setCreateUserName(currentUser.getName());
                opt.setCreateTime(LocalDateTime.now());
                accountStatementOptService.save(opt);
            }
    }

    /**
     * 添加操作内容
     *
     * @param asId       对账单ID
     * @param asNo       对账单号
     * @param oriData    原数据
     * @param updateData 变更数据
     * @param user       操作人
     */
    @Override
    public void addOptContentRecord(Long asId, String asNo, List<AccountStatementDetail> oriData, List<AccountStatementDetail> updateData, CurrentUserDTO user) {
        Map<Long, String> map = Maps.newHashMap();
        for(AccountStatementDetail od: oriData){
            Long odAsdId = od.getId();
            for(AccountStatementDetail ud: updateData){
                if(odAsdId.equals(ud.getId())){
                    map.put(odAsdId, this.handleUpdateContent(od, ud));
                }
            }
        }

        List<AccountStatementOpt> optList = Lists.newArrayList();
        for (Map.Entry<Long, String> in : map.entrySet()) {
            AccountStatementOpt opt = new AccountStatementOpt();
            opt.setAsId(asId);
            opt.setAsNo(asNo);
            opt.setAsdId(in.getKey());
            opt.setUpdateContent(in.getValue());
            opt.setOptType(2);
            opt.setCreateUserId(user.getUserId());
            opt.setCreateUserCode(user.getLoginName());
            opt.setCreateUserName(user.getName());
            opt.setCreateTime(LocalDateTime.now());
            optList.add(opt);
        }

            if(CollectionUtils.isNotEmpty(optList)){
                accountStatementOptService.saveBatch(optList);
            }
    }

    /**
     * 处理变更字段的记录
     *
     * @param od 更新前的数据
     * @param ud 更新后的数据
     * @return 结果
     */
    private String handleUpdateContent(AccountStatementDetail od, AccountStatementDetail ud){
        JSONArray rows = new JSONArray();

        // 对账数量
        if(0 != od.getAsQuantity().compareTo(ud.getAsQuantity())){
            JSONObject item = new JSONObject();
            item.put("updateField", "asQuantity");
            item.put("updateFieldName", "对账数量");
            item.put("oriValue", od.getAsQuantity());
            item.put("currValue", ud.getAsQuantity());
            rows.add(item);
        }

        // 含税单价
        if(0 != od.getTaxPrice().compareTo(ud.getTaxPrice())){
            JSONObject item = new JSONObject();
            item.put("updateField", "taxPrice");
            item.put("updateFieldName", "含税单价");
            item.put("oriValue", od.getTaxPrice());
            item.put("currValue", ud.getTaxPrice());
            rows.add(item);
        }

        // 税率
        if(od.getTaxValue().equals(ud.getTaxValue())){
            JSONObject item = new JSONObject();
            item.put("updateField", "taxValue");
            item.put("updateFieldName", "税率");
            item.put("oriValue", "(" + od.getTaxCode() + ")" + od.getTaxValue());
            item.put("currValue", "(" + ud.getTaxCode() + ")" + ud.getTaxValue());
            rows.add(item);
        }

        return rows.toJSONString();
    }
}
