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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.ctop.finance.policy.entity.CwjsSaleAssistantInfo;
import org.jeecg.ctop.finance.policy.service.ICwjsSaleAssistantInfoService;
import org.jeecg.modules.ctop.entity.ProjectMember;
import org.jeecg.modules.system.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 财务-2021-10-30 17:50
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-10-18
 */
@Slf4j
@Api(tags = "销售与销售助理关系")
@RestController
@RequestMapping("/cwjsSaleAssistantInfo")
public class CwjsSaleAssistantInfoController {
    @Autowired
    private ICwjsSaleAssistantInfoService cwjsSaleAssistantInfoService;


    @Autowired
    private ISysRoleService roleService;


    /**
     * 分页列表查询
     *
     * @param cwjsSaleAssistantInfo
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "销售与销售助理关系-分页列表查询", notes = "销售与销售助理对应关系-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CwjsSaleAssistantInfo>> queryPageList(CwjsSaleAssistantInfo cwjsSaleAssistantInfo,
                                                              @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                              HttpServletRequest req) {
        Result<IPage<CwjsSaleAssistantInfo>> result = new Result<>();
        QueryWrapper<CwjsSaleAssistantInfo> queryWrapper = QueryGenerator.initQueryWrapper(cwjsSaleAssistantInfo, req.getParameterMap());
        queryWrapper.eq("del_flag", 0);
        queryWrapper.orderByDesc("update_time");
        Page<CwjsSaleAssistantInfo> page = new Page<CwjsSaleAssistantInfo>(pageNo, pageSize);
        IPage<CwjsSaleAssistantInfo> pageList = cwjsSaleAssistantInfoService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 添加
     *
     * @param cwjsSaleAssistantInfo
     * @return
     */
    @ApiOperation(value = "销售与销售助理对应关系-添加", notes = "销售与销售助理对应关系-添加")
    @PostMapping(value = "/add")
    public Result<CwjsSaleAssistantInfo> add(@RequestBody CwjsSaleAssistantInfo cwjsSaleAssistantInfo) {
        Result<CwjsSaleAssistantInfo> result = new Result<>();
        try {
            cwjsSaleAssistantInfo.setCreateTime(new Date());
            cwjsSaleAssistantInfoService.save(cwjsSaleAssistantInfo);
            result.success("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.error500("操作失败");
        }
        return result;
    }

    /**
     * 编辑
     *
     * @param cwjsSaleAssistantInfo
     * @return
     */
    @ApiOperation(value = "销售与销售助理对应关系-编辑", notes = "销售与销售助理对应关系-编辑")
    @PostMapping(value = "/edit")
    public Result<CwjsSaleAssistantInfo> edit(@RequestBody CwjsSaleAssistantInfo cwjsSaleAssistantInfo) {
        Result<CwjsSaleAssistantInfo> result = new Result<CwjsSaleAssistantInfo>();
        CwjsSaleAssistantInfo cwjsSaleAssistantInfoEntity = cwjsSaleAssistantInfoService.getById(cwjsSaleAssistantInfo.getId());
        if (cwjsSaleAssistantInfoEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = cwjsSaleAssistantInfoService.updateById(cwjsSaleAssistantInfo);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "销售与销售助理对应关系-通过id删除", notes = "销售与销售助理对应关系-通过id删除")
    @GetMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id") String id) {
        try {
            cwjsSaleAssistantInfoService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败", e.getMessage());
            return Result.error("删除失败!");
        }
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "销售与销售助理对应关系-批量删除", notes = "销售与销售助理对应关系-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<CwjsSaleAssistantInfo> deleteBatch(@RequestParam(name = "ids") String ids) {
        Result<CwjsSaleAssistantInfo> result = new Result<>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.cwjsSaleAssistantInfoService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "销售与销售助理对应关系-通过id查询", notes = "销售与销售助理对应关系-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<CwjsSaleAssistantInfo> queryById(@RequestParam(name = "id", required = true) String id) {
        Result<CwjsSaleAssistantInfo> result = new Result<>();
        CwjsSaleAssistantInfo cwjsSaleAssistantInfo = cwjsSaleAssistantInfoService.getById(id);
        if (cwjsSaleAssistantInfo == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(cwjsSaleAssistantInfo);
            result.setSuccess(true);
        }
        return result;
    }


    @ApiOperation(value = "销售/销售助理下拉框", notes = "销售/销售助理下拉框")
    @GetMapping(value = "/getSaleListByUserDepart")
    public Result<IPage<CwjsSaleAssistantInfo>> getSaleListByUserDepart(@RequestParam(name = "userId") String userId) {
        //查询用户角色
        String roleCode = roleService.getRoleCodeByUserId(userId);
        //管理员 查询全部
        if (StringUtils.equals("admin", roleCode) || StringUtils.equals("finance", roleCode) || StringUtils.equals("policyReview", roleCode)) {
            userId = null;
        }
        return cwjsSaleAssistantInfoService.getSaleListByUserDepart(userId);

    }


    @ApiOperation(value = "政策新增-销售回显下拉框", notes = "政策新增-销售回显下拉框")
    @GetMapping(value = "/getCwSaleAndAssistantList")
    public Result getCwSaleAndAssistantList(@RequestParam(name = "userId") String userId) {
        List<CwjsSaleAssistantInfo> list = cwjsSaleAssistantInfoService.getCwSaleAndAssistantList(userId);
        return Result.successMsg("查询成功。", list);
    }


    @ApiOperation(value = "政策-筛选广告主", notes = "政策-筛选广告主")
    @GetMapping(value = "/cwjsParticipateList")
    public Result cwjsParticipateList(@RequestBody ProjectMember projectMember) {
        return cwjsSaleAssistantInfoService.cwjsParticipateList(projectMember);

    }


}
