package org.jeecg.ctop.finance.settlement.controller;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.ctop.finance.policy.service.impl.CwjsPolicyInfoServiceImpl;
import org.jeecg.ctop.finance.policy.service.impl.CwjsSaleAssistantInfoServiceImpl;
import org.jeecg.ctop.finance.policy.utils.Check;
import org.jeecg.ctop.finance.settlement.entity.pojo.CtopCwjsSettlementInfo;


import org.jeecg.ctop.finance.settlement.service.ISettlementInfoService;
import org.jeecg.modules.ctop.service.IMaterialReportOverViewService;
import org.jeecg.modules.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;


/**
 * Administrator
 * 2021/7/28
 **/

@Slf4j
@RestController
@RequestMapping("/settLementController")
public class SettlementController {

    @Autowired
    private ISettlementInfoService settlementInfoService;

    @Autowired
    private ISysRoleService sysRoleService;


    @Autowired
    private IMaterialReportOverViewService materialReportOverViewService;


    @Autowired
    private CwjsSaleAssistantInfoServiceImpl cwjsSaleAssistantInfoService;

    @ApiOperation(value="财务结算-账户列表查询", notes="财务结算-账户列表查询")
    @GetMapping(value = "/getSettlementList")
    public Result getSettlementList(CtopCwjsSettlementInfo settlementInfo,
                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        //查询用户角色
        String roleCode = sysRoleService.getRoleCodeByUserId(settlementInfo.getCreateUserId());
        //查询所有包含自己的下级
        //Set<String> subordinates= materialReportOverViewService.recursiveQuerySubordinate(settlementInfo.getCreateUserId());
        List<String> userIds = new ArrayList<>();
        if (!"admin".equals(roleCode)){
            //查询 销售及助理 关联对应的人员
            userIds = cwjsSaleAssistantInfoService.getCwSaleAndAssistantListStr(settlementInfo.getCreateUserId());
            if (Check.isNull(userIds)){
                userIds.add(settlementInfo.getCreateUserId());
            }
        }
        settlementInfo.setUserIds(new HashSet<>(userIds));
       return settlementInfoService.getSettlementList(settlementInfo,pageNo,pageSize);
    }


    @ApiOperation(value="财务结算-修改账户状态账户(0-开 1-关)", notes="财务结算-修改账户状态账户(0-开 1-关)")
    @GetMapping(value = "/updSettlementStatusById")
    public Result updSettlementStatusById(@RequestParam Long id,@RequestParam Integer status) {
       return settlementInfoService.updSettlementStatusById(id,status);
    }


    @ApiOperation(value="财务结算-删除", notes="财务结算-删除")
    @GetMapping(value = "/deleteSettlementById")
    public Result deleteSettlementById(@RequestParam Long id) {
       return settlementInfoService.deleteSettlementById(id);
    }


    @ApiOperation(value="财务结算-上传账户", notes="财务结算-上传账户")
    @PostMapping("/settlementAccountImport")
    public Result settlementAccountImport(CtopCwjsSettlementInfo settlementInfo, @RequestParam("file") MultipartFile files) {
        if (files.isEmpty()){
            return Result.errorMsg("附件不能为空！");
        }
        return settlementInfoService.settlementAccountImport(settlementInfo,files);
    }


    @Autowired
    private CwjsPolicyInfoServiceImpl cwjsPolicyInfoService;


    @ApiOperation(value="财务结算-获取负责人下拉列表", notes="财务结算-获取负责人下拉列表")
    @GetMapping("/getResponsibleUserByIds")
    public Result getResponsibleUserByIds(@RequestParam("userId") String userId) {
        //查询用户角色
        String roleCode = sysRoleService.getRoleCodeByUserId(userId);
        //普通用户
        Set<String> ids = new HashSet<String>(){{
            add(userId);
        }};
        /*if ("admin".equals(roleCode)){
            //查询所有销售 && 媒介
            List allSaleList = cwjsPolicyInfoService.getAllSaleAndMeidaManager();
            return Result.successMsg("查询所有销售和媒介用户",allSaleList);

        }else if ("saleDirector".equals(roleCode) || //销售总监
                "kuaishou_operations_director".equals(roleCode) || //快手运营总监
                "toutiao_operations_manager".equals(roleCode) || // 头条运营经理
                "externalOperationManager".equals(roleCode) || //外部运营经理
                "genera_manager_of_operations".equals(roleCode) || //运营总经理
                "toutiao_operations_director".equals(roleCode) || //头条运营总监
                "kuaishou_operations_manager".equals(roleCode) //快手运营经理
         ){
            //查询所有包含自己的下级
           // ids = materialReportOverViewService.recursiveQuerySubordinate(userId);
            //查询 销售及助理 关联对应的人员
            List<String> saleIds = cwjsSaleAssistantInfoService.getCwSaleAndAssistantListStr(userId);
            if (!Check.isNull(ids)){
                ids.addAll(new HashSet<>(saleIds));
            }

        }*/
        if ("admin".equals(roleCode)){
            //查询所有销售 && 媒介
            List allSaleList = cwjsPolicyInfoService.getAllSaleAndMeidaManager();
            return Result.successMsg("查询所有销售和媒介用户",allSaleList);

        }else {
            //查询所有包含自己的下级
            // ids = materialReportOverViewService.recursiveQuerySubordinate(userId);
            //查询 销售及助理 关联对应的人员
            List<String> saleIds = cwjsSaleAssistantInfoService.getCwSaleAndAssistantListStr(userId);
            if (!Check.isNull(saleIds)){
                ids.addAll(new HashSet<>(saleIds));
            }

        }


        List<Map<String,Object>> list = settlementInfoService.getUserByIds(ids);
        return Result.successMsg("查询用户",list);
    }

    @ApiOperation(value="财务结算-获取所有运营人员", notes="财务结算-获取所有运营人员")
    @GetMapping("/getAllOperationUser")
    public Result getAllOperationUser() {
        List<Map<String,Object>> list = cwjsPolicyInfoService.getAllOperationUser();
        return Result.successMsg("查询运营用户列表",list);
    }
}