package cn.com.ty.lift.enterprise.region.controller;

import cn.com.ty.lift.enterprise.region.dao.entity.Region;
import cn.com.ty.lift.enterprise.region.dao.entity.model.RegionRequest;
import cn.com.ty.lift.enterprise.region.dao.mapper.RegionMapper;
import cn.com.ty.lift.enterprise.region.service.RegionService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
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 java.util.List;
import java.util.Map;

/**
 * <p>
 * 区域表 前端控制器
 * </p>
 *
 * @author wang-hai-cheng
 * @since 2019-12-09
 */
@AllArgsConstructor
@RestController
@RequestMapping("/region")
public class RegionController {
    private final RegionService regionService;
    private final RegionMapper regionMapper;

    @PostMapping("/list")
    public RestResponse list(@RequestBody RegionRequest regionRequest) {
        return regionService.regions(regionRequest);
    }

    /**
     * 区域信息
     *
     * @param region 区域id
     * @return RestResponse 区域信息
     */
    @PostMapping
    public RestResponse region(@RequestBody Region region) {
        Region byId = regionService.getById(region.getId());
        if (null == byId) {
            return RestResponse.success();
        }
        return RestResponse.success(byId, "成功");
    }

    /**
     * 新增区域信息
     *
     * @param region 区域信息
     * @return RestResponse 操作结果
     */
    @PostMapping("/add")
    public RestResponse add(@RequestBody Region region) {
        if (regionService.save(region)) {
            return RestResponse.success(null, "成功");
        }
        return RestResponse.fail();
    }

    /**
     * 更新区域信息
     *
     * @param region 区域信息
     * @return RestResponse 操作结果
     */
    @PostMapping("/update")
    public RestResponse update(@RequestBody Region region) {
        return regionService.updateByRegionId(region);
    }

    @PostMapping("/delete")
    public RestResponse delete(@RequestBody Region region) {
        return regionService.delete(region);
    }

    /**
     * 项目模块，查询区域列表
     *
     * @param region mtCompanyId维保公司id
     * @return RestResponse 区域列表
     * @since 2019/12/17 10:35
     */
    @PostMapping("/project/regions")
    public RestResponse regions(@RequestBody Region region) {
        List<Region> regions = regionService.list(new QueryWrapper<Region>()
                .select("id", "area_name")
                .eq("mt_company_id", region.getMtCompanyId()));
        if (regions.isEmpty()) {
            return RestResponse.success();
        }
        return RestResponse.success(regions, "成功");
    }

    /**
     * 项目模块，查询区列表
     *
     * @param region 区域id
     * @return RestResponse
     * @since 2019/12/17 10:34
     */
    @PostMapping("/project/areas")
    public RestResponse areas(@RequestBody Region region) {
        Region byId = regionService.getById(region.getId());
        if (null == byId) {
            return RestResponse.success();
        }
        String cityCode = byId.getCityCode();
        List<Map<String, Object>> areas = regionService.areas(cityCode);
        if (areas.isEmpty()) {
            return RestResponse.success();
        }
        return RestResponse.success(areas, "成功");
    }

    /**
     * 区域属于的省市名，省市编码
     *
     * @param region 区域id
     * @return RestResponse
     * @since 2019/12/19 15:15
     */
    @PostMapping("/project/areaInfo")
    public RestResponse projectAreaInfo(@RequestBody Region region) {
        return regionService.projectRegions(region);
    }

    /**
     * 项目模块，根据regionId查询区域主管，区域文员
     *
     * @param region 区域id
     * @return RestResponse 查询区域主管，区域文员
     * @since 2019/12/17 10:33
     */
    @PostMapping("/user")
    public RestResponse users(@RequestBody Region region) {
        return regionService.selectRegionUser(region);
    }

    @PostMapping("wx/list")
    public RestResponse regionIdAndName(@RequestBody Region region) {
        return RestResponse.success(regionMapper.selectIdAndNameLikeName(region.getMtCompanyId(), "%" + region.getAreaName() + "%"));
    }
}