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

import cn.com.ty.lift.common.model.CountPage;
import cn.com.ty.lift.common.utils.ValuePool;
import cn.com.ty.lift.common.verify.Val;
import cn.com.ty.lift.common.verify.Validation;
import cn.com.ty.lift.common.verify.Validate;
import cn.com.ty.lift.enterprise.oa.dto.LiftCertificateRequest;
import cn.com.ty.lift.enterprise.oa.dto.LiftCertificateResponse;
import cn.com.ty.lift.enterprise.oa.entity.LiftCertificate;
import cn.com.ty.lift.enterprise.oa.service.LiftCertificateService;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.validation.Valid;


/**
 * <p>
 *      操作证 前端控制器
 * </p>
 *
 * @author wcz
 * @since 2019-12-04
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/oa/liftCertificate")
public class LiftCertificateController {

    private LiftCertificateService liftCertificateService;

    /**
     * 根据id查找
     * @param request LiftCertificateRequest
     * @return RestResponse
     */
    @PostMapping("findOne")
    @Validation(fields = {"id"})
    public RestResponse findOne(@Val @RequestBody LiftCertificateRequest request) {
        LiftCertificateResponse result = liftCertificateService.infoById(request);
        return RestResponse.success(result);
    }

    /**
     * 根据条件查询
     * @param request LiftCertificateRequest
     * @return RestResponse
     */
    @PostMapping("list")
    @Validation(fields = "mtCompanyId")
    public RestResponse list(@Val @RequestBody LiftCertificateRequest request) {
        //维保工操作证 :按照有效期，由近及远排序
        request.addDesc("expiration_date");
        IPage<LiftCertificateResponse> pages = liftCertificateService.pageByCondition(request);
        long count = liftCertificateService.countByCondition(request);
        return RestResponse.success(CountPage.getCountPage(count, pages));
    }

    /**
     * 更新操作证信息
     * @param entity LiftCertificate
     * @return RestResponse
     */
    @PostMapping("modify")
    @Validation(fields = {"id", "ownerId", "mtCompanyId", "code", "certificateType",
            "expirationDate", "issuanceAgency", "firstImgUrl", "secondImgUrl"})
    public RestResponse modify(@Val @RequestBody LiftCertificateRequest entity) {
        Long id = entity.getId();
        Long ownerId = entity.getOwnerId();
        Long mtCompanyId = entity.getMtCompanyId();
        LiftCertificate certificate = liftCertificateService.findByUserAndMtCompany(id, ownerId, mtCompanyId);
        Validate.notNull(certificate,ValuePool.LIFT_CERT_NOT_EXIST);

        certificate.setCode(entity.getCode());
        certificate.setCertificateType(entity.getCertificateType());
        certificate.setExpirationDate(entity.getExpirationDate());
        certificate.setIssuanceAgency(entity.getIssuanceAgency());
        certificate.setFirstImgUrl(entity.getFirstImgUrl());
        certificate.setSecondImgUrl(entity.getSecondImgUrl());
        certificate.setStatus(3);

        boolean result = liftCertificateService.updateById(certificate);
        return RestResponse.success(result);
    }

    /**
     * 操作证审核
     * @param request LiftCertificateRequest
     * @return RestResponse
     */
    @PostMapping("audit")
    @Validation(fields = {"id", "pass"})
    public RestResponse audit(@Val @RequestBody LiftCertificateRequest request) {
        //0：无证，1：待审核，2：审核未通过，3：审核通过，4：超期
        LiftCertificate liftCertificate = liftCertificateService.getById(request.getId());
        Validate.notNull(liftCertificate, ValuePool.LIFT_CERT_NOT_EXIST);
        Validate.equals(liftCertificate.getStatus(), 1, ValuePool.LIFT_CERT_MUST_TO_AUDIT);
        liftCertificate.setStatus(request.isPass() ? 3 : 2);
        boolean result = liftCertificateService.saveOrUpdate(liftCertificate);
        return RestResponse.success(result);
    }

    /**
     * 新增操作证
     * @param entity LiftCertificate
     * @return RestResponse
     */
    @PostMapping("add")
    public RestResponse add(@Valid @RequestBody LiftCertificate entity) {
        Long ownerId = entity.getOwnerId();
        Long mtCompanyId = entity.getMtCompanyId();

        int count = liftCertificateService.countByUserAndMtCompany(ownerId, mtCompanyId);
        Validate.notTrue(count > 0, ValuePool.LIFT_CERT_HAD_EXIST);
        entity.setStatus(1);

        boolean result = liftCertificateService.saveOrUpdate(entity);
        return RestResponse.success(result);

    }
    @PostMapping("findByUser")
    @Validation(fields = {"ownerId","mtCompanyId"})
    public RestResponse findByUser(@Val @RequestBody LiftCertificateRequest request){
        LiftCertificate entity = liftCertificateService.findByUserAndMtCompany(request.getId(), request.getOwnerId(), request.getMtCompanyId());
        return RestResponse.success(entity);
    }
}
