package com.tofly.watermeter.custom.controller.meterBusiness;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.watermeter.custom.entity.SearchSb;
import com.tofly.watermeter.entity.Bwrepair;
import com.tofly.watermeter.entity.Bwrksbdatail;
import com.tofly.watermeter.service.BwrepairService;
import com.tofly.watermeter.service.BwrksbdatailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author FlashWJL
 * @date 2021/8/23 11:55
 */
@RestController
@AllArgsConstructor
@RequestMapping("/repair")
@Api(tags = "表务--水表维修管理")
public class SbRepairController {

    private final BwrksbdatailService bwrksbdatailService;
    private final BwrepairService bwrepairService;


    @GetMapping("/getScarp")
    @ApiOperation(value = "查看报废水表")
    public ResultRespone getScarp(Page page, SearchSb searchSb) {
        Long cj = searchSb.getBwSbcj();
        Long lx = searchSb.getBwSblx();
        Long kj = searchSb.getBwSbkj();
        String input = searchSb.getInputContent();
        IPage<Bwrksbdatail> bwrksbdatailIPage = bwrksbdatailService.page(page, Wrappers.<Bwrksbdatail>lambdaQuery()
                .eq(Bwrksbdatail::getBwSbzt, "61")
                .eq(StringUtil.isNotNull(cj), Bwrksbdatail::getBwSbcj, cj)
                .eq(StringUtil.isNotNull(kj), Bwrksbdatail::getBwSbkj, kj)
                .eq(StringUtil.isNotNull(lx), Bwrksbdatail::getBwSblx, lx)
                .and(StringUtil.isNotEmpty(input),
                        k -> k.like(Bwrksbdatail::getBwSbbh, input)
                                .or().like(Bwrksbdatail::getBwLsbh, input)).orderByDesc(Bwrksbdatail::getBwSbzt));
        return ResultRespone.success(bwrksbdatailIPage);
    }

    @PostMapping("/repair")
    @ApiOperation(value = "新增维修记录")
    @Transactional(rollbackFor = Exception.class)
    public ResultRespone saveRepair(@RequestBody Bwrepair bwrepair) {
        String sbbh = bwrepair.getSbbh();
        String repairResult = bwrepair.getRepairResult();
        Long repairUser = bwrepair.getRepairUser();
        Date repairTime = bwrepair.getRepairTime();
        if (StringUtil.isEmpty(sbbh)) {
            throw new ToflyDeniedException("请填写水表编号");
        }
        if (StringUtil.isEmpty(repairResult)) {
            throw new ToflyDeniedException("请填写维修结果");
        }
        if (StringUtil.isNull(repairUser)) {
            throw new ToflyDeniedException("请选择维修人员");
        }
        if (StringUtil.isNull(repairTime)) {
            throw new ToflyDeniedException("请选择维修时间");
        }
        Bwrksbdatail bwrksbdatail = bwrksbdatailService.getOne(Wrappers.<Bwrksbdatail>lambdaQuery().eq(Bwrksbdatail::getBwSbbh, sbbh)
                .eq(Bwrksbdatail::getBwSbzt, "61").select(Bwrksbdatail::getBwSbbh));
        if (StringUtil.isNull(bwrksbdatail)) {
            throw new ToflyDeniedException("请正确填写水表编号");
        }
        //如果维修后合格了；则修改水表为已维修状态
        if ("1".equals(repairResult)) {
            bwrksbdatailService.update(Wrappers.<Bwrksbdatail>lambdaUpdate().eq(Bwrksbdatail::getBwSbbh, sbbh).set(Bwrksbdatail::getBwSbzt, "950"));
        }
        return ResultRespone.success(bwrepairService.save(bwrepair));
    }

    @GetMapping("/getRepair")
    @ApiOperation(value = "维修记录查看")
    public ResultRespone getRepair(Page page, SearchSb searchSb) {
        return ResultRespone.success(bwrepairService.getRepair(page, searchSb));
    }

    @PostMapping("/updateRepair")
    @ApiOperation(value = "修改维修记录(水表编号不能修改；请禁用水表编号这一项)")
    @Transactional(rollbackFor = Exception.class)
    public ResultRespone updateRepair(@RequestBody Bwrepair bwrepair) {
        Long id = bwrepair.getId();
//        String sbbh = bwrepair.getSbbh();
        String repairResult = bwrepair.getRepairResult();
        String repairDesciption = bwrepair.getRepairDescription();
        Long repairUser = bwrepair.getRepairUser();
        Date repairTime = bwrepair.getRepairTime();
        Bwrepair repair = bwrepairService.getOne(Wrappers.<Bwrepair>lambdaQuery().eq(Bwrepair::getId, id).select(Bwrepair::getSbbh));
        if (StringUtil.isNull(repair)) {
            throw new ToflyDeniedException("请正确选择维修记录");
        }
        String sbbh = repair.getSbbh();
        //修改维修记录；不能修改水表编号
        bwrepairService.update(Wrappers.<Bwrepair>lambdaUpdate().eq(Bwrepair::getId, id)
                .set(StringUtil.isNotEmpty(repairDesciption), Bwrepair::getRepairDescription, repairDesciption)
                .set(StringUtil.isNotEmpty(repairResult), Bwrepair::getRepairResult, repairResult)
                .set(StringUtil.isNotNull(repairUser), Bwrepair::getRepairUser, repairUser)
                .set(StringUtil.isNotNull(repairTime), Bwrepair::getRepairTime, repairTime));
        //修改水表状态：已维修、报废
        bwrksbdatailService.update(Wrappers.<Bwrksbdatail>lambdaUpdate().eq(Bwrksbdatail::getBwSbbh, sbbh)
                .set("1".equals(repairResult), Bwrksbdatail::getBwSbzt, "950")
                .set("0".equals(repairResult), Bwrksbdatail::getBwSbzt, "61"));
        return ResultRespone.success(true);
    }

    @DeleteMapping("/deleteRepair/{ids}")
    @ApiOperation(value = "删除维修记录")
    @Transactional(rollbackFor = Exception.class)
    public ResultRespone deleteRepair(@PathVariable("ids") String ids) {
        if (StringUtil.isEmpty(ids)){
            throw new ToflyDeniedException("请选择维修记录");
        }
        List<String> idList = Arrays.asList(ids.split(","));
        List<Bwrepair> bwrepairList = bwrepairService.list(Wrappers.<Bwrepair>lambdaQuery()
                .in(Bwrepair::getId , idList).select(Bwrepair::getSbbh));
        if (StringUtil.isNotNull(bwrepairList)){
            List<String> sbbhList = bwrepairList.stream().map(Bwrepair::getSbbh).collect(Collectors.toList());
            //删除维修记录
            bwrepairService.removeByIds(idList);
            //恢复水表为报废状态
            bwrksbdatailService.update(Wrappers.<Bwrksbdatail>lambdaUpdate().in(Bwrksbdatail::getBwSbbh, sbbhList)
                    .set(Bwrksbdatail::getBwSbzt, "61"));
        }
        return ResultRespone.success(true);
    }


}
