package com.chinaweal.aiccs.aiccs.seriousillegal.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.abnormal.entity.Gzaiccode;
import com.chinaweal.aiccs.aiccs.abnormal.service.IGzaiccodeService;
import com.chinaweal.aiccs.aiccs.seriousillegal.entity.TSForIllOperatingList;
import com.chinaweal.aiccs.aiccs.seriousillegal.service.TSForIllOperatingListService;
import com.chinaweal.aiccs.aiccs.system.entity.TRBaseCode;
import com.chinaweal.aiccs.aiccs.system.service.TRBaseCodeService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.constant.Constant;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.org.service.AicorgService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 拟列入严重违法失信企业清单 前端控制器
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-03-17
 */
@Api("拟列入严重违法失信企业清单接口")
@RestController
@RequestMapping("/forIllList")
public class ForIllOperatingListController extends BaseController {

    @Autowired
    private TRBaseCodeService baseCodeService;
    @Autowired
    private TSForIllOperatingListService forIllOperatingListService;
    @Autowired
    private IGzaiccodeService iGzaiccodeService;
    @Autowired
    private AicorgService aicorgService;

    /**
     * 拟列入严重违法失信企业清单分页
     *
     * @param pageRequestDto 分页参数
     * @return 分页结果
     * @author lroyia
     * @since 2021年3月17日 13:52:40
     */
    @ApiOperation("拟列入严重违法失信企业清单分页")
    @PostMapping("getForIllListPage")
    public RestResult<?> getForIllListPage(HttpServletRequest request, @RequestBody @ApiParam("分页参数") PageRequestDto pageRequestDto) {
        AssertUtils.isNotNull(pageRequestDto);
        pageRequestDto.checkDefault(1, 10, "lauptime");
        Map<String, TRBaseCode> causeMap = baseCodeService.getBaseCodeMapFormCache("CD71");
        Page<TSForIllOperatingList> page = new Page<>(pageRequestDto.getCurPage(), pageRequestDto.getPageSize());
        page.addOrder(OrderItem.asc(pageRequestDto.getOrderField()));
        Map<String, Object> customParamMap = pageRequestDto.getCustomParamMap();
        String searchUscc = StringUtils.tranObject(customParamMap.get("searchUscc"));
        if (StringUtils.isNotBlank(searchUscc)) {
            //判断入参为统一码还是注册号
            if (StringUtils.isUscc(searchUscc)) {
                customParamMap.put("uniscid", searchUscc);
            } else {
                customParamMap.put("regno", searchUscc);
            }
        }
        AICUser curUser = getLoginUser(request);

        //判断是否注册二处的
        if ("f7b2117a-ee80-4470-930e-03f106f3e031".equals(curUser.getUnitID())) {
            customParamMap.put("acceptRegorg", "440129");//查询受理机关
        } else {
            String code = aicorgService.getRegionCodeByUser(curUser);
            customParamMap.put("regorg", code);//查询登记机关
        }
        Page<TSForIllOperatingList> data = forIllOperatingListService.listRelationEnt(page, customParamMap);
        for (TSForIllOperatingList record : data.getRecords()) {
            TRBaseCode causeCode = causeMap.get(record.getSpeCause());
            if (causeCode != null) {
                record.setSpeCauseStr(causeCode.getName());
            }
        }

        return RestResult.ok(data);
    }

    /**
     * 移除拟列入预告清单信息
     *
     * @param ids 移除的数据id，多个半角逗号隔开
     * @return 移除结果
     * @author lroyia
     * @since 2021年3月17日 16:04:10
     */
    @ApiOperation("移除拟列入预告清单信息")
    @PostMapping("removeForIllList")
    public RestResult<?> removeForIllList(@ApiParam("移除的数据id，多个半角逗号隔开") String ids) {
        AssertUtils.isNotBlank(ids);
        forIllOperatingListService.update(new TSForIllOperatingList()
                        .setLauptime(LocalDateTime.now())
                        .setOperationStatus("3"),
                new LambdaUpdateWrapper<TSForIllOperatingList>()
                        .in(TSForIllOperatingList::getSerialId, ids.split(Constant.COMMA)));
        return OK;
    }
}

