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

import cn.com.ctop.common.module.service.ISysUserService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.entity.SysUser;
import org.jeecg.common.system.vo.LoginUser;
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.CtopCwjsSettlementFileInfo;
import org.jeecg.ctop.finance.settlement.entity.vo.SettlementFileParamsVo;
import org.jeecg.ctop.finance.settlement.service.ISettlementFileInfoService;
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 java.util.*;


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

@Slf4j
@RestController
@RequestMapping("/settLementFileController")
public class SettlementFileController {
    @Autowired
    private ISettlementFileInfoService settlementFileInfoService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private IMaterialReportOverViewService materialReportOverViewService;


    @Autowired
    private CwjsSaleAssistantInfoServiceImpl cwjsSaleAssistantInfoService;


    @ApiOperation(value="结算单-结算单列表查询", notes="结算单-结算单列表查询")
    @GetMapping(value = "/getSettlementSheetList")
    public Result<PageInfo<Map<String,String>>> getSettlementSheetList(SettlementFileParamsVo paramsVo,
                                                                       @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                       @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
        //查询用户角色 -master
        String roleCode = sysRoleService.getRoleCodeByUserId(paramsVo.getCreateUserId());
        List<String> userIds = new ArrayList<>();
        //管理员 || 财务
        if ("admin".equals(roleCode) || "finance".equals(roleCode)){
            paramsVo.setSaleUserIds(new HashSet<>(userIds));
            paramsVo.setCreateUserId(null);
        }else {
            //查询 销售及助理 关联对应的人员
          /*  userIds = cwjsSaleAssistantInfoService.getCwSaleAndAssistantListStr(paramsVo.getCreateUserId());
            if (Check.isNull(userIds)){
                userIds.add(paramsVo.getCreateUserId());
            }*/
            //查询所有包含自己的下级
            Set<String> operatorUserIds= materialReportOverViewService.recursiveQuerySubordinate(paramsVo.getCreateUserId());
            paramsVo.setSaleUserIds(operatorUserIds);
        }
       return settlementFileInfoService.getSettlementSheetList(paramsVo,pageNo,pageSize);
    }


    @ApiOperation(value="结算单-删除", notes="结算单-删除")
    @GetMapping(value = "/deleteSettlementFile")
    public Result deleteSettlementFile(SettlementFileParamsVo paramsVo) {
        return settlementFileInfoService.deleteSettlementFile(paramsVo);
    }

    @ApiOperation(value="结算单-账户截图上传", notes="结算单-账户截图上传")
    @PostMapping(value = "/insertAccountImages")
    public Result insertAccountImages(@RequestBody CtopCwjsSettlementFileInfo settlementFileInfo) {
        if (Check.isNull(settlementFileInfo.getSettlementList())){
            return Result.errorMsg("账户截图不能为空！");
        }
        return settlementFileInfoService.insertAccountImages(settlementFileInfo);
    }

    @ApiOperation(value="结算单-查询账户数量以及图片数量", notes="结算单-查询账户数量以及图片数量")
    @PostMapping(value = "/getAccountNumAndImgNum")
    public Result getAccountNumAndImgNum(@RequestBody SettlementFileParamsVo paramsVo) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        SysUser user = userService.getById(loginUser.getId());
        return settlementFileInfoService.getAccountNumAndImgNum(paramsVo,user);
    }
    @Autowired
    private ISysUserService userService;

}