package com.yijia.station.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.yijia.common.core.domain.entity.SysDept;
import com.yijia.common.core.domain.entity.SysUser;
import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.station.domain.StationOilGun;
import com.yijia.station.domain.StationOilPrice;
import com.yijia.station.service.IStationOilGunService;
import com.yijia.station.service.IStationOilPriceService;
import com.yijia.system.service.ISysDeptService;
import com.yijia.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.station.domain.StationOilPriceAdjust;
import com.yijia.station.service.IStationOilPriceAdjustService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 油品调价信息Controller
 *
 * @author yijia
 * @date 2020-12-09
 */
@RestController
@RequestMapping("/station/adjust")
public class StationOilPriceAdjustController extends BaseController
{
    @Autowired
    private IStationOilPriceAdjustService stationOilPriceAdjustService;
    @Autowired
    private IStationOilPriceService stationOilPriceService;
    @Autowired
    private IStationOilGunService stationOilGunService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询油品调价信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(StationOilPriceAdjust stationOilPriceAdjust)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = sysDeptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            stationOilPriceAdjust.setStationIdList(list);
            stationOilPriceAdjust.setStationId(null);
        }
        startPage();
        List<StationOilPriceAdjust> adjustList = stationOilPriceAdjustService.selectStationOilPriceAdjustList(stationOilPriceAdjust);
        return getDataTable(adjustList);
    }

    /**
     * 导出油品调价信息列表
     */
    @Log(title = "油品调价信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(StationOilPriceAdjust stationOilPriceAdjust)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = sysDeptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            stationOilPriceAdjust.setStationIdList(list);
            stationOilPriceAdjust.setStationId(null);
        }
        List<StationOilPriceAdjust> adjustList = stationOilPriceAdjustService.selectStationOilPriceAdjustList(stationOilPriceAdjust);
        ExcelUtil<StationOilPriceAdjust> util = new ExcelUtil<StationOilPriceAdjust>(StationOilPriceAdjust.class);
        return util.exportExcel(adjustList, "adjust");
    }

    /**
     * 获取油品调价信息详细信息
     */
    @GetMapping(value = "/{adjustPriceId}")
    public AjaxResult getInfo(@PathVariable("adjustPriceId") Long adjustPriceId)
    {
        return AjaxResult.success(stationOilPriceAdjustService.selectStationOilPriceAdjustById(adjustPriceId));
    }

    /**
     * 新增油品调价信息
     */
    @Log(title = "油品调价信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StationOilPriceAdjust stationOilPriceAdjust) throws ParseException {
        Date date=new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = sdf.format(date);
        Date  now = sdf.parse(time);
        stationOilPriceAdjust.setAdjustDate(now);
        if(stationOilPriceAdjust.getTakeEffectStatus()!=null && stationOilPriceAdjust.getTakeEffectStatus().equals("1")){
            stationOilPriceAdjust.setTakeEffectDate(now);
        }
        LoginUser currentUser = SecurityUtils.getLoginUser();
        stationOilPriceAdjust.setOperator(currentUser.getUsername());
        stationOilPriceAdjust.setStatus("1");
        stationOilPriceAdjust.setStationId(currentUser.getUser().getDeptId());
       int i= stationOilPriceAdjustService.insertStationOilPriceAdjust(stationOilPriceAdjust);
       //查询出刚添加的油品调价信息
        if(stationOilPriceAdjust.getTakeEffectStatus()!=null && stationOilPriceAdjust.getTakeEffectStatus().equals("1")){
            List<StationOilPriceAdjust> list = stationOilPriceAdjustService.selectStationOilPriceAdjustList(stationOilPriceAdjust);
            for(StationOilPriceAdjust adjust: list ){
                //修改油品价格根据油品价格id
                StationOilPrice oilPrice=new StationOilPrice();
                oilPrice.setOilPriceId(adjust.getOilPriceId());
                oilPrice.setOilPrice(adjust.getOilAdjustPrice());
                oilPrice.setDate(now);
                stationOilPriceService.updateStationOilPrice(oilPrice);
                //修改油枪信息中的价格
                StationOilGun gun =new StationOilGun();
                gun.setStationId(adjust.getStationId());
                gun.setOilName(adjust.getOilName());
                List<StationOilGun> listGun = stationOilGunService.selectStationOilGunList(gun);
                for(StationOilGun oilGun: listGun ){
                    StationOilGun stationOilGun =new StationOilGun();
                    stationOilGun.setOilPrice(adjust.getOilAdjustPrice());
                    stationOilGun.setOilGunId(oilGun.getOilGunId());
                    stationOilGun.setDate(now);
                    stationOilGunService.updateStationOilGun(stationOilGun);
                }
                StationOilPriceAdjust priceAdjust=new StationOilPriceAdjust();
                priceAdjust.setStatus("2");
                priceAdjust.setAdjustPriceId(adjust.getAdjustPriceId());
                stationOilPriceAdjustService.updateStationOilPriceAdjust(priceAdjust);
            }
        }
        return toAjax(i);
    }

    /**
     * 修改油品调价信息
     */
    @Log(title = "油品调价信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StationOilPriceAdjust stationOilPriceAdjust)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        stationOilPriceAdjust.setStationId(currentUser.getUser().getDeptId());
        return toAjax(stationOilPriceAdjustService.updateStationOilPriceAdjust(stationOilPriceAdjust));
    }

    /**
     * 删除油品调价信息
     */
    @Log(title = "油品调价信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{adjustPriceIds}")
    public AjaxResult remove(@PathVariable Long[] adjustPriceIds)
    {
        return toAjax(stationOilPriceAdjustService.deleteStationOilPriceAdjustByIds(adjustPriceIds));
    }


}
