package com.dayou.controller;

import cn.hutool.core.util.ObjectUtil;
import com.dayou.dto.*;
import com.dayou.exception.ErrorCode;
import com.dayou.vo.AssetsBusinessMember;
import com.dayou.vo.AssetsOrderFundInvoiceVO;
import com.dayou.vo.AssetsRepertoryVo;
import com.dayou.vo.AssetsVO;
import com.dayou.workflow.annotation.FinishTask;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IAssetsService;
import com.dayou.entity.Assets;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;
import com.dayou.common.PullDownModel;
import com.dayou.utils.ConvertUtil;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import javax.servlet.http.HttpServletResponse;

/**
 * 资产业务订单表
 *
 * @author GouGengquan
 * @since 2024-03-28
 */
@RestController
@RequestMapping("assets")
@Slf4j
public class AssetsController extends BaseController {
    @Autowired
    private IAssetsService assetsService;

    /**
     * 条件查询资产任务列表
     * @param page 分页条件
     * @param assetsSelectDTO 查询条件
     * @return RestResponse<Page<AssetsVO>>
     */
    @GetMapping("/selectPage")
    public RestResponse<Page<AssetsVO>> selectPage(AssetsSelectDTO assetsSelectDTO, Page page){
        return RestResponse.data(assetsService.selectPage(page,assetsSelectDTO));
    }

    /**
     * 条件查询我的资产订单列表
     * @param page 分页条件
     * @param assetsSelectDTO 查询条件
     * @return Page<AssetsVO>
     */
    @GetMapping("/selectMyOrderPage")
    public RestResponse<Page<AssetsVO>> selectMyOrderPage(AssetsSelectDTO assetsSelectDTO, Page page){
        return RestResponse.data(assetsService.selectMyOrderPage(page,assetsSelectDTO));
    }

    /**
     * 导出我的资产订单列表
     * @param assetsSelectDTO 查询条件
     */
    @GetMapping("/exportMyOrder")
    public void exportMyOrder(AssetsSelectDTO assetsSelectDTO, HttpServletResponse response ) throws IOException {
        exportPlus(response,"资产我的订单",assetsService.exportMyOrder(assetsSelectDTO),AssetsVO.class);
    }

    /**
     * 条件查询资产已归档列表
     * @param page 分页条件
     * @param assetsSelectDTO 查询条件
     * @return Page<AssetsVO>
     */
    @GetMapping("/selectArchivedPage")
    public RestResponse<Page<AssetsVO>> selectArchivedPage(AssetsSelectDTO assetsSelectDTO, Page page) {
        return RestResponse.data(assetsService.selectArchivedPage(page, assetsSelectDTO));
    }

    /**
     * 条件查询资产已终止列表
     * @param page 分页条件
     * @param assetsSelectDTO 查询条件
     * @return Page<AssetsVO>
     */
    @GetMapping("/selectTerminatedPage")
    public RestResponse<Page<AssetsVO>> selectTerminatedPage(Page page, AssetsSelectDTO assetsSelectDTO) {
        return RestResponse.data(assetsService.selectTerminatedPage(page, assetsSelectDTO));
    }

    /**
     * 条件查询资产入库列表
     * @param page 分页条件
     * @param assetsSelectDTO 查询条件
     * @return Page<AssetsVO>
     */
    @GetMapping("/selectRepertoryPage")
    public RestResponse<Page<AssetsRepertoryVo>> selectRepertoryPage(Page page, AssetsSelectDTO assetsSelectDTO) {
        return RestResponse.data(assetsService.selectRepertoryPage(page, assetsSelectDTO));
    }

    /**
     * 资产入库数据导出
     * @param assetsSelectDTO 查询dto
     * @param response response
     * @throws IOException io异常
     */
    @GetMapping("/repertoryExport")
    public void repertoryExport(AssetsSelectDTO assetsSelectDTO, HttpServletResponse response ) throws IOException {
        List<AssetsRepertoryVo> list = assetsService.selectRepertoryList(assetsSelectDTO);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (ObjectUtil.isNotNull(list) && !list.isEmpty()){
            exportPlus(response,"资产入库统计数据_" + dateFormat.format(new Date()),list,AssetsRepertoryVo.class);
        }else {
            ErrorCode.throwBusinessException("条件筛选结果数量为0,不可导出！", "500");
        }
    }

    /**
     * 资产业务订单表详情
     */
    @GetMapping("getAssetsDetailById/{id}")
    public RestResponse<AssetsVO> getAssetsDetailById(@PathVariable Long id){
        return RestResponse.data(assetsService.getAssetsDetailById(id));
     }

    /**
     * 资产业务订单新增
     */
    @PostMapping("/add")
    public RestResponse<WorkNodeCommit> save(@RequestBody Assets assets) {
        return RestResponse.data(assetsService.add(assets));
    }

    /**
     * 资产业务订单表更新
     */
    @PutMapping("/update")
    public RestResponse<Boolean> update(@RequestBody Assets assets) {
        Boolean ret = assetsService.update(assets);
        return RestResponse.data(ret);
    }

    /**
     * 资产业务订单表删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = assetsService.delete(id);
        return RestResponse.data(ret);
    }


    /**
     * 资产业务订单表下拉列表
     */
    @GetMapping("/simpleAll")
    public RestResponse<List<PullDownModel>> simpleAll(){
        LambdaQueryWrapper<Assets> lambdaQueryWrapper = new LambdaQueryWrapper<Assets>()
        .select(Assets::getId,Assets::getName)
        .eq(Assets::getDeleted,false);
        List<Assets> list= assetsService.list(lambdaQueryWrapper);
        List<PullDownModel> ret=ConvertUtil.copyList(list, PullDownModel.class);
        return RestResponse.data(ret);
    }

    /**
     * 修改资产业务订单项目负责人与项目成员
     * @param taskRecordDTO 修改dto
     * @return RestResponse<Boolean>
     */
    @FinishTask
    @PutMapping("/updatePrincipalAndMembers")
    public RestResponse<Boolean> updatePrincipalAndMembers(@RequestBody TaskRecordDTO<AssetsPrincipalMembersDTO> taskRecordDTO) {
        return RestResponse.data(assetsService.updatePrincipalAndMembers(taskRecordDTO));
    }

    /**
     * 根据业务id获取资产业务款项信息
     * @param id 业务id
     * @return RestResponse<AssetsOrderFundInvoiceVO>
     */
    @GetMapping("/getOrderFundInvoiceById/{id}")
    public RestResponse<AssetsOrderFundInvoiceVO> getOrderFundInvoiceById(@PathVariable Long id) {
        return RestResponse.data(assetsService.getOrderFundInvoiceById(id));
    }

    /**
     * 资产业务意见书产品取号
     * @param dto 取号dto
     * @return RestResponse<String>
     */
    @PostMapping("/takeAssetsStatementProductionNo")
    public RestResponse<Boolean> takeAssetsStatementProductionNo(@RequestBody TakeAssetsProductionNoDTO dto) {
        return RestResponse.data(assetsService.takeAssetsProductionNo(dto));
    }

    /**
     * 资产业务报告产品取号
     * @param dto 取号dto
     * @return RestResponse<String>
     */
    @FinishTask
    @PostMapping("/takeAssetsReportProductionNo")
    public RestResponse<Boolean> takeAssetsReportProductionNo(@RequestBody TaskRecordDTO<TakeAssetsProductionNoDTO> dto) {
        return RestResponse.data(assetsService.takeAssetsProductionNo(dto.getTaskData()));
    }

    /**
     * 获取资产项目所有参与人
     * @param assetsBusinessMembersDTO 查询dto
     * @return List<AssetsBusinessMembers>
     */
    @GetMapping("/getAssetsBusinessMembers")
    public RestResponse<List<AssetsBusinessMember>> getAssetsBusinessMembers(AssetsBusinessMembersDTO assetsBusinessMembersDTO) throws JsonProcessingException {
        return RestResponse.data(assetsService.getAssetsBusinessMembers(assetsBusinessMembersDTO));
    }

    /**
     * 获取资产市场下单部门
     * @return RestResponse<List<PullDownModel>>
     */
    @GetMapping("/getAssetsMarketDepartment")
    public RestResponse<List<PullDownModel>> getAssetsMarketDepartment(){
        List<PullDownModel> ret = ConvertUtil.copyList(assetsService.getAssetsMarketDepartment(), PullDownModel.class);
        return RestResponse.data(ret);
    }

    @GetMapping("/detail/{id}")
    public RestResponse<Assets> detail(@PathVariable Long id) {
        Assets assets = assetsService.detail(id);
        return RestResponse.data(assets);
    }

    /**
     * 更新资产订单基本信息
     * @param assets 信息
     * @return RestResponse<Boolean>
     */
    @PutMapping("/updateOrderBaseInfo")
    public RestResponse<Boolean> updateOrderBaseInfo(@RequestBody Assets assets) {
        return RestResponse.data(assetsService.updateOrderBaseInfo(assets));
    }
}

