package com.storlead.sales.controller.target;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.BusinessDeleteDTO;
import com.storlead.sales.pojo.dto.target.TargetPlanAddDTO;
import com.storlead.sales.pojo.dto.target.TargetPlanScopeSettingDTO;
import com.storlead.sales.pojo.dto.target.TargetPlanSearchDTO;
import com.storlead.sales.pojo.entity.target.TargetQuotaEntity;
import com.storlead.sales.service.customer.MessageAssembleService;
import com.storlead.sales.service.target.TargetPlanService;
import com.storlead.sales.service.target.TargetQuotaService;
import com.storlead.user.service.IDepartService;
import com.storlead.user.service.ISubCompanyService;
import com.storlead.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 目标方案主表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-12-11
 */
@RestController
@RequestMapping("/target-plan")
@Api(tags = "销售: 业绩目标管理相关接口")
public class TargetPlanController {

    @Resource
    private TargetPlanService targetPlanService;
    @Resource
    private TargetQuotaService targetQuotaService;
    @Resource
    private MessageAssembleService assembleService;
    @Resource
    private IDepartService deptService;
    @Resource
    private IUserService userService;
    @Resource
    private ISubCompanyService subCompanyService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "业绩目标管理-新增方案配置", notes = "业绩目标管理-新增方案配置")
    public Result<?> add(@RequestBody TargetPlanAddDTO targetPlanAddDTO) {
        return targetPlanService.addPlan(targetPlanAddDTO);
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "业绩目标管理-修改方案配置", notes = "业绩目标管理-修改方案配置")
    public Result<?> edit(@RequestBody TargetPlanAddDTO targetPlanAddDTO) {
        return targetPlanService.editPlan(targetPlanAddDTO);
    }

    @PostMapping(value = "/scope-setting")
    @ApiOperation(value = "业绩目标管理-添加/编辑参与范围设置", notes = "业绩目标管理-添加/编辑参与范围设置")
    public Result<?> addScopeSetting(@RequestBody TargetPlanScopeSettingDTO targetPlanScopeSettingDTO) {

        return targetPlanService.addScopeSetting(targetPlanScopeSettingDTO);

    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "业绩目标管理-目标方案列表", notes = "业绩目标管理-目标方案列表")
    public Result<?> list(@RequestBody TargetPlanSearchDTO targetPlanSearchDTO) {
        return targetPlanService.selectListVO(targetPlanSearchDTO);
    }

    @GetMapping(value = "/get-by-id")
    @ApiOperation(value = "业绩目标管理-通过id查看目标方案详情", notes = "业绩目标管理-通过id查看目标方案详情")
    public Result<?> getById(@RequestParam(name="id",required=false) Long id) {

        return targetPlanService.getTargetPlanVOById(id);
    }

    @ApiOperation(value = "业绩目标管理-删除", notes = "业绩目标管理-删除")
    @PostMapping("delete")
    public Result delete(@RequestBody BusinessDeleteDTO businessDeleteDTO) {
        boolean deleteOk = false;
        if(!ObjectUtils.isEmpty(businessDeleteDTO.getId())) {
            deleteOk = targetPlanService.lgDelete(businessDeleteDTO.getId());
        }
        if(!deleteOk) {
            return Result.error("删除失败");
        }
//        LambdaUpdateWrapper<TargetQuotaEntity> wrapper = new LambdaUpdateWrapper<>();
//        wrapper.eq(TargetQuotaEntity::getTargetPlanId,businessDeleteDTO.getId());
//        wrapper.set(TargetQuotaEntity::getIsDelete, CommonConstant.DEL_FLAG_1);
//        targetQuotaService.update(wrapper);

        //配置删除
        LambdaUpdateWrapper<TargetQuotaEntity> targetQuotaWrapper = new LambdaUpdateWrapper<>();
        targetQuotaWrapper.eq(TargetQuotaEntity::getTargetPlanId,businessDeleteDTO.getId());
        targetQuotaWrapper.eq(TargetQuotaEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        List<TargetQuotaEntity> targetQuotaEntityList = targetQuotaService.list(targetQuotaWrapper);
        if(!CollectionUtils.isEmpty(targetQuotaEntityList)) {
            targetQuotaEntityList.forEach(targetQuotaEntity -> {targetQuotaEntity.setIsDelete(CommonConstant.DEL_FLAG_1);});
            targetQuotaService.saveOrUpdateBatch(targetQuotaEntityList);

            Set<Long> userIds = new HashSet<>();
//            List<TargetQuotaEntity> subTargetQuotaList = targetQuotaEntityList.stream().filter(e->e.getOrganizationalType() == 2).collect(Collectors.toList());
//            if (!CollectionUtils.isEmpty(subTargetQuotaList)){
//                List<Long> subCompanyIdList = subTargetQuotaList.stream().map(e->e.getOrganizationalId()).collect(Collectors.toList());
//                Set<Long> companyLeaderIdList = subCompanyService.list(new LambdaQueryWrapper<SubCompanyEntity>().in(SubCompanyEntity::getId,subCompanyIdList)).stream().map(SubCompanyEntity::getLeaderId).collect(Collectors.toSet());
//                userIds.addAll(companyLeaderIdList);
//            }
//            List<TargetQuotaEntity> deptTargetQuotaList = targetQuotaEntityList.stream().filter(e->e.getOrganizationalType() == 3).collect(Collectors.toList());
//            if (!CollectionUtils.isEmpty(deptTargetQuotaList)){
//                List<Long> deptIdList = deptTargetQuotaList.stream().map(e->e.getOrganizationalId()).collect(Collectors.toList());
//                Set<Long> deptLeaderIdList = deptService.list(new LambdaQueryWrapper<DeptEntity>().in(DeptEntity::getId,deptIdList)).stream().map(DeptEntity::getManagerId).collect(Collectors.toSet());
//                userIds.addAll(deptLeaderIdList);
//            }
            List<TargetQuotaEntity> userTargetQuotaList = targetQuotaEntityList.stream().filter(e->e.getOrganizationalType() == 4).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(userTargetQuotaList)){
                Set<Long> userIdList = userTargetQuotaList.stream().map(e->e.getOrganizationalId()).collect(Collectors.toSet());
                userIds.addAll(userIdList);
            }
            assembleService.sendTargetPlanEventMessages(businessDeleteDTO.getId(), "CONFIG_DELETE" ,userIds,null);

        }
        return Result.result("删除成功");
    }

}
