package io.renren.modules.business.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
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.BusinessDictService;
import io.renren.modules.business.service.BusinessProjectService;
import io.renren.modules.business.service.BusinessWorkerService;
import org.apache.commons.lang.StringUtils;
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-21 15:36:25
 */
@RestController
@RequestMapping("business/businessproject")
public class BusinessProjectController {
    @Autowired
    private BusinessProjectService businessProjectService;
    @Autowired
    private BusinessWorkerService businessWorkerService;
    @Autowired
    private BusinessDictService businessDictService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:businessproject:list")
    public R list(@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("/allList")
    @RequiresPermissions("business:businessproject:list")
    public R allList(){
        //System.out.println(params);
        List<BusinessProjectEntity> page = businessProjectService.allList();
        return R.ok().put("page", page);
    }

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

    /**
     * 获取字典信息
     */
    @RequestMapping("/dictList")
    @RequiresPermissions("business:businessproject: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);
    }

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

    //获取统计的数
    @RequestMapping("/getTotal")
    @RequiresPermissions("business:businessproject:list")
    public R getTotal(@RequestParam Map<String, Object> params){
        //System.out.println(params);
        List<Map<String,Object>> page = businessProjectService.getTotal(params);
        return R.ok().put("page", page);
    }

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

    /**
     * 合同管理所用特定条件检索列表   建设单位ID + 年份  已废
     */
    @RequestMapping("/searchList")
    @RequiresPermissions("business:businessproject:list")
    public R searchList(@RequestParam Map<String, Object> params){
        System.out.println(params);
        List<BusinessProjectEntity> page = businessProjectService.querySearchList(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{projectId}")
    @RequiresPermissions("business:businessproject:info")
    public R info(@PathVariable("projectId") Integer projectId){
		BusinessProjectEntity businessProject = businessProjectService.getById(projectId);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:businessproject:save")
    public R save(@RequestBody BusinessProjectEntity businessProject){
        if(businessProject.getProjectId() == null || businessProject.getProjectId() == 0) {
            businessProject.setProjectNewName(businessProject.getProject());
            businessProjectService.save(businessProject);
        } else {
            // 修改时，不改原来的项目名
            if(StringUtils.isNotBlank(businessProject.getProject())) {
                businessProject.setProject(null);
            }
            businessProjectService.updateById(businessProject);
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:businessproject:update")
    public R update(@RequestBody BusinessProjectEntity businessProject){
		businessProjectService.updateById(businessProject);

        return R.ok();
    }

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

        return R.ok();
    }

}
