package io.renren.modules.business.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.business.entity.BusinessContractEntity;
import io.renren.modules.business.entity.BusinessDictEntity;
import io.renren.modules.business.entity.BusinessProjectEntity;
import io.renren.modules.business.entity.BusinessWorkerEntity;
import io.renren.modules.business.service.BusinessContractService;
import io.renren.modules.business.service.BusinessDictService;
import io.renren.modules.business.service.BusinessProjectService;
import io.renren.modules.business.service.BusinessWorkerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 合同信息
 *
 * @author jiaqiao
 * @email jiaqiao@163.com
 * @date 2022-08-22 17:16:07
 */
@RestController
@RequestMapping("business/businesscontract")
public class BusinessContractController {
    @Autowired
    private BusinessContractService businessContractService;
    @Autowired
    private BusinessProjectService businessProjectService;
    @Autowired
    private BusinessDictService businessDictService;
    @Autowired
    private BusinessWorkerService businessWorkerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:businesscontract:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = businessContractService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取字典信息
     */
    @RequestMapping("/dictList")
    @RequiresPermissions("business:businesscontract:list")
    public R dictList(@RequestParam Map<String, Object> params){
        // PageUtils page = businessDictService.queryPage(params);
        List<BusinessDictEntity> businessDictEntity = businessDictService.listWithTree();
        return R.ok().put("data", businessDictEntity);
    }

    /**
     * 立项列表
     */
    @RequestMapping("/projectList")
    @RequiresPermissions("business:businesscontract:list")
    public R projectList(@RequestParam Map<String, Object> params){
//        PageUtils page = businessProjectService.queryPage(params);
        List<BusinessProjectEntity> page = businessProjectService.quaryListNoPage(params);
        //System.out.println(page);
        return R.ok().put("page", page);
    }

    /**
     * 立项列表
     */
    @RequestMapping("/projectListPc")
    @RequiresPermissions("business:businesscontract:list")
    public R projectListPc(@RequestParam Map<String, Object> params){
        PageUtils page = businessProjectService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 立项负责工人idList
     */
    @RequestMapping("/ddIdList")
    @RequiresPermissions("business:businesscontract:list")
    public R ddIdList(){
        //System.out.println(params);
        List<Integer> ddIdList = businessProjectService.ddIdList();
        return R.ok().put("page", ddIdList);
    }

    /**
     * 列表  所有未离职人员列表 不含有翻页
     */
    @RequestMapping("/tcList")
    @RequiresPermissions("business:businesscontract:list")
    public R tcList(@RequestParam Map<String, Object> params) {
        List<BusinessWorkerEntity> page = businessWorkerService.queryNewPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 根据传入的ids查询ids含有的所有的项目信息
     */
    @RequestMapping("/projectList/{ids}")
    @RequiresPermissions("business:businesscontract:list")
    public R projectList(@PathVariable("ids") String ids){
        //System.out.println(params);
        List<BusinessProjectEntity> page = businessProjectService.queryPerojectList(ids);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{contractId}")
    @RequiresPermissions("business:businesscontract:info")
    public R info(@PathVariable("contractId") Integer contractId) {
        BusinessContractEntity businessContract = businessContractService.getById(contractId);

        return R.ok().put("businessContract", businessContract);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:businesscontract:save")
    public R save(@RequestBody BusinessContractEntity businessContract) {
        businessContractService.save(businessContract);
        String[] ids = businessContract.getProjectId().split(",");
        businessProjectService.changeState(ids);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:businesscontract:update")
    public R update(@RequestBody BusinessContractEntity businessContract) {
        businessContractService.updateById(businessContract);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:businesscontract:delete")
    public R delete(@RequestBody Integer[] contractIds) {
        businessContractService.removeByIds(Arrays.asList(contractIds));

        return R.ok();
    }

}
