package com.chinaweal.aiccs.aiccs.market.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.abnormal.service.IGzaiccodeService;
import com.chinaweal.aiccs.aiccs.market.entity.AddressAssign;
import com.chinaweal.aiccs.aiccs.market.service.AddressAssignService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.solr.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
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 lombok.extern.slf4j.Slf4j;
import org.joda.time.LocalDate;
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 com.chinaweal.aiccs.common.base.controller.BaseController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sh
 * @since 2023-02-17
 */
@Api(tags = "地址认领相关接口")
@RestController
@RequestMapping("/addressAssign")
@Slf4j
public class AddressAssignController extends BaseController {
    @Resource
    private AddressAssignService addressAssignService;
    @Resource
    private IGzaiccodeService iGzaiccodeService;


    @ApiOperation("删除认领地址")
    @PostMapping("delAddress")
    public RestResult<?> delAddress(String assignId, HttpServletRequest request){
        AICUser loginUser = getLoginUser(request);
        if(loginUser==null){
            throw new BusinessException("登录超时，请重新登录");
        }

        AddressAssign addressAssign = addressAssignService.getOne(new LambdaQueryWrapper<AddressAssign>().eq(AddressAssign::getId, assignId));
        addressAssignService.updateById(addressAssign.setRemOperator(loginUser.getName()).setRemTime(LocalDateTime.now()));
        return RestResult.ok();
    }
    @ApiOperation("获取认领地址")
    @PostMapping("getAddressList")
    public RestResult<?> getAddressList(@ApiParam("分页参数") @RequestBody PageRequestDto pageRequestDto, HttpServletRequest request) {
        AssertUtils.isNotNull(pageRequestDto);
        pageRequestDto.checkDefault(1, 10, null);
        AICUser loginUser = getLoginUser(request);
        if(loginUser==null){
            throw new BusinessException("登录超时，请重新登录");
        }
        String regionID = loginUser.getRegionID();
        if("440101".equals(regionID)){
            throw new BusinessException("市局账号暂无该功能");
        }

        Map<String, Object> paramMap = pageRequestDto.getParamMap();
        paramMap.put("orgNumber", regionID);

        Object searchDate = paramMap.get("searchDate");
        if (searchDate instanceof List) {
            List<String> list = (List<String>) searchDate;
            paramMap.put("startDate", list.get(0));
            //paramMap.put("endDate", list.get(1));
            paramMap.put("endDate", LocalDate.parse(list.get(1)).plusDays(1).toString());
        }

        IPage<AddressAssign> addressAssignIPage = addressAssignService.getAddressAssignList(new Page<>(pageRequestDto.getCurPage(), pageRequestDto.getPageSize()), pageRequestDto.getParamMap());

        addressAssignIPage.getRecords().forEach(item -> {
            item.setOrgNumber(iGzaiccodeService.getNameByCode(item.getOrgNumber()));
        });
        return RestResult.ok(addressAssignIPage);
    }


    @ApiOperation("新增认领地址")
    @PostMapping("addAddress")
    public RestResult<?> addAddress(String address, String org_number, String type, HttpServletRequest request) {
        AssertUtils.isNotNull(address);
        AICUser user = getLoginUser(request);
        if(user==null){
            throw new BusinessException("登录超时，请重新登录");
        }

        Map<String, Object> resultMap = new HashMap<>();

        if(user.getRegionID().length() == 6 && StringUtils.isBlank(org_number)){
            throw new BusinessException("区级账号需指定监管所");
            //return RestResult.ok(resultMap.put("msg","区级账号需指定监管所"));
        }else if(user.getRegionID().length() == 8){
            org_number = user.getRegionID();
        }

        AddressAssign atom = new AddressAssign().setId(UUID.randomUUID().toString())
                .setAddress(address)
                .setOrgNumber(org_number)
                .setOperator(user.getName())
                .setInsertTime(LocalDateTime.now())
                .setState("0");

        if(StringUtils.isNotBlank(type)){
            atom.setType(type);
        }else{
            atom.setType("0");
        }
        addressAssignService.save(atom);

        return RestResult.ok();
    }
}

